小编Ste*_*fan的帖子

Hibernate拦截器 - 为什么在onSave之后调用onFlushDirty?

计划

我正在使用Hibernate为一个小项目实现createDate和lastUpdate时间戳.我使用一个EmptyInterceptor并根据我在这里找到的建议解决方案重载提供的方法.解决方案除了一个小细节外,工作正常.我想添加一个列,指示对象是否已经更新.我知道我可以通过简单比较两个创建和更新的时间戳是否存在差异来实现这一点,但我需要让此字段指示存在更新.

我使用onSave方法,在存储新对象时调用该方法将wasUpdated值设置为'N',表示没有更新.在onFlushDirty()方法中,我将此值设置为"Y".

问题

我想说当我创建并持久保存新的Object时,createDate和lastUpdate字段具有相同的Date,但wasUpdated字段设置为'N',因为没有更新.我只在我的代码中使用session.save(),没有session.update(),也没有session.saveOrUpdate().Hibernate的日志表明实际上有一个Update,它将wasUpdated值设置为'Y'.

什么可以作为此更新的来源?它在哪里被触发?

对象初始化和持久性

我在hibernate.cfg.xml中禁用了自动提交.

<property name="hibernate.connection.autocommit">false</property>
Run Code Online (Sandbox Code Playgroud)

这是我创建对象的方式:

ExampleObject ex = new ExampleObject();
ex.setValue("TestStringValue");
this.session = HibernateUtil.getSessionFactory().openSession();
this.session.beginTransaction();
this.session.save(ex);
this.session.getTransaction().commit();
this.session.close();
Run Code Online (Sandbox Code Playgroud)

拦截器

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


    if (entity instanceof TimeStamped) {

        Date insertDate = new Date();
        int indexOfCreateDateColumn = ArrayUtils.indexOf(propertyNames, "createdDate");
        int indexOfUpdatedDateColumn = ArrayUtils.indexOf(propertyNames, "lastUpdatedDate");
        int indexOfWasUpdated = ArrayUtils.indexOf(propertyNames, "wasUpdated");

        state[indexOfCreateDateColumn] =insertDate;
        state[indexOfUpdatedDateColumn] =insertDate;
        state[indexOfWasUpdated] ='N';

        return true;
    }
    return false;
    }
Run Code Online (Sandbox Code Playgroud)

第二种方法是设置lastUpdatedDate并将wasUpdated字段设置为'Y'.

@Override …
Run Code Online (Sandbox Code Playgroud)

java state hibernate updates interceptor

9
推荐指数
2
解决办法
9873
查看次数

Pip:连接因“ProtocolError”而中断

我正在尝试在Ubuntu 20.04.5pip上的全新虚拟环境中安装软件包,但当我第二次运行时,我不断收到以下警告。第一次尝试后软件包安装失败。pip

\n
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by \'ProtocolError(\'Connection aborted.\', FileNotFoundError(2, \'No such file or directory\'))\': /simple/pip/\nWARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by \'ProtocolError(\'Connection aborted.\', FileNotFoundError(2, \'No such file or directory\'))\': /simple/pip/\nWARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by \'ProtocolError(\'Connection aborted.\', FileNotFoundError(2, \'No such file or directory\'))\': /simple/pip/\nWARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by \'ProtocolError(\'Connection aborted.\', FileNotFoundError(2, \'No such file …
Run Code Online (Sandbox Code Playgroud)

python pip python-3.x

4
推荐指数
1
解决办法
5731
查看次数

标签 统计

hibernate ×1

interceptor ×1

java ×1

pip ×1

python ×1

python-3.x ×1

state ×1

updates ×1