2014-06-23 43 views
0

我重写了Hibernate EmptyInterceptor来捕获一些属性字段来做一些装饰。Hibernate Interceptor onSave和onLoad方法返回null

公共类MyEntityInterceptor扩展EmptyInterceptor我需要访问

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

实体字段返回null。请让我知道您的反馈,我在这里错过了什么?

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); 
} 

回答

0

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

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

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

捕获属性不正确。对象[]状态,字符串[] propertyNames是关键

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

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

然后您将预期值。让我知道你是否需要进一步的帮助。