【问题标题】:Hibernate Interceptor onSave and onLoad methods returns nullHibernate 拦截器的 onSave 和 onLoad 方法返回 null
【发布时间】:2014-06-23 20:14:21
【问题描述】:

我重写了 Hibernate EmptyInterceptor 来捕获一些属性字段来做一些装饰。

public class MyEntityInterceptor extends EmptyInterceptor我需要访问

保存前和读取时的字段。我的 onSave 和 onLoad 方法如下。但是我的

实体字段返回空值。请让我知道您的反馈以及我在这里遗漏了什么?

public boolean onLoad(Object entity,Serializable id, Object[] state,String[] propertyNames, Type[] types)
        throws CallbackException{

    Persistable entity = (Persistable) entity;
    System.out.println("===============>"+entity.getCreatedBy());//returns null
    return true;
}



public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {

    if (entity instanceof IPersistable) {
        IPersistable entity = (IPersistable) entity;
        System.out.println("Created By=================>"+((IPersistable) entity).getCreatedBy());//Returns null

    }

    return (true);
}

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    在初始化对象之前调用的 onLoad 方法。请参阅 API。

    http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Interceptor.html。实体将是一个

    类的未初始化的空实例。这就是您得到空输出的原因。你的方式

    捕获属性不正确。 Object[] state,String[] propertyNames 是关键

    如果你想处理传递参数,应该使用参数。状态自带

    值列表和属性名称表示字段列表。请迭代 state[] 数组,

    然后您将获得预期值。如果您需要进一步的帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2012-09-26
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多