0
假设我有两个类Foo和Bar。 富有多到一个协会酒吧这样映射:Hibernate拦截器,修改onSave中的当前状态导致SQL更新
<many-to-one name="bar" class="Bar" lazy="false" fetch="join" >
<column name="BAR_ID" sql-type="INTEGER" />
</many-to-one>
我已经创建了一个从EmptyInterceptor延伸的自定义拦截器,并覆盖的onSave方法是这样:
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
boolean modified = false;
if(entity instanceof Foo) {
Bar bar = new Bar();
//do some initialization stuff to bar
for(int i = 0; i < propertyNames.length; i++) {
if("bar".equals(propertyNames[i])) {
state[i] = bar;
modified = true;
break;
}
}
//This code essentially gets the current hibernate session and calls save on
//the passed object.
BarDAO barDao = BarDAO.INSTANCE;
barDAO.save(bar);
}
return modified;
}
的问题是,尽管SQL插入语句的酒吧,一个SQL更新语句执行的我为实体对象执行,这是一个Foo,并在插入之前进行了修改。这会导致以下Hibernate异常。
最后一个原因:批量更新从更新中返回了意外的行数 [0];实际行数:0;预期:1
如果我不保存栏对象,则按预期执行SQL插入语句。
任何有关可能出错的信息?
在此先感谢。