Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate拦截器,在onSave中修改当前状态将导致SQL更新_Java_Hibernate_Interceptor - Fatal编程技术网

Java Hibernate拦截器,在onSave中修改当前状态将导致SQL更新

Java Hibernate拦截器,在onSave中修改当前状态将导致SQL更新,java,hibernate,interceptor,Java,Hibernate,Interceptor,假设我有两个类Foo和BarFoo与Bar的多对一关联映射如下: <many-to-one name="bar" class="Bar" lazy="false" fetch="join" > <column name="BAR_ID" sql-type="INTEGER" /> </many-to-one> public boolean onSave(Object entity, Serializable id, Object[] state,

假设我有两个类FooBarFooBar的多对一关联映射如下:

<many-to-one name="bar" class="Bar" lazy="false" fetch="join" >
    <column name="BAR_ID" sql-type="INTEGER" />
</many-to-one>
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;
} 

我创建了一个自定义拦截器,它从EmptyInterceptor扩展,并覆盖onSave方法,如下所示:

<many-to-one name="bar" class="Bar" lazy="false" fetch="join" >
    <column name="BAR_ID" sql-type="INTEGER" />
</many-to-one>
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;
} 
public boolean onSave(对象实体、可序列化id、对象[]状态、,
字符串[]属性名称,类型[]类型){
布尔修改=假;
if(Foo的实体实例){
条形=新条形();
//做一些初始化的事情吧
for(int i=0;i
问题在于,尽管对bar执行SQL insert语句,但对实体对象执行SQL update语句,实体对象是一个Foo,在插入之前已被修改。这将导致以下Hibernate异常

最后一个原因:批量更新从更新返回意外的行数 [0]; 实际行数:0;预期:1

如果不保存bar对象,则会按预期执行SQL insert语句

对可能出现的问题有什么见解吗


提前感谢。

我认为您需要在拦截器中的onFlushDirty方法中更新currentState数组

有一个类似的问题,因为我正在修改onLoad和onFlushDirty方法中的对象。问题是hibernate认为对象的前一个状态是由onLoad方法产生的,update语句试图使用这些值来查找db表中的行。可能是相同的问题,因为您正在修改onSave方法中的状态