使用Hibernate PreInsertEventListener更改实体 [英] Change entity using Hibernate PreInsertEventListener

查看:278
本文介绍了使用Hibernate PreInsertEventListener更改实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于以下文章,我正在使用Hibernate 4.1尝试调用PreInsertEventListener来更新实体,然后再将其插入数据库中:

I am using Hibernate 4.1 trying to call a PreInsertEventListener to update the entity before inserting it into the database, based on the article here: http://anshuiitk.blogspot.ca/2010/11/hibernate-pre-database-opertaion-event.html

public class PreInsertListener implements PreInsertEventListener {

    @Override
    public boolean onPreInsert(PreInsertEvent event) {
        Product product  = (Product)event.getEntity();
        String barcode = "B" + product.getProductId();
        product.setBarcode(barcode);

        // Update the state value that will be persisted
        String[] properties = event.getPersister().getEntityMetamodel().getPropertyNames();
        List<String> propertiesList = Arrays.asList(properties);
        event.getState()[propertiesList.indexOf('barcode')] = barcode;
    }
}

当我调试它时,它正在执行PreInsertListener代码,但是插入数据库中的值不包含代码中的更改.这曾经在Hibernate 3中有效.在这里我想念什么?

When I debug it, it is executing the PreInsertListener code, but the values inserted into the database do not contain the changes from the code. This used to work in Hibernate 3. What am I missing here?

推荐答案

请确保您不属于上面共享的博客文章中列出的方法3问题.如果您在一个事务中对实体执行插入和更新操作,则更新操作将覆盖您的preInsert侦听器操作.

Just make sure that you are not falling into the approach-3 problem listed on the blog post that you shared above. If you have insert and update actions on your entity in one single transaction, then your preInsert listener actions will be overridden by the update action.

-来自博客文章的消息( http ://anshuiitk.blogspot.ca/2010/11/hibernate-pre-database-opertaion-event.html )

-- Message from the blog post ( http://anshuiitk.blogspot.ca/2010/11/hibernate-pre-database-opertaion-event.html)

Hibernate生成一个准备好的语句,并从事件中存在的'state'数组中填充参数.因此,对此状态"数组所做的任何更改都会反映在由休眠生成的sql语句中,并最终反映在数据库中.插入和更新事件具有此状态数组的不同副本.

Hibernate generates a prepared statement and fills in the parameters from the 'state' array present in the event. Hence any changes made to the this 'state' array are reflected in the sql statement generated by the hibernate and finally on the database. The insert and update events have a different copy of this states array.

在预更新事件之前(如果同时发生插入和更新),将调用预插入侦听器.当实体在同一事务中创建,持久保存然后进行修改时,就会发生这种情况.这将导致在同一实体上产生两个单独的sql语句,第一个将是insert语句,第二个将是update语句.使用insert语句时,我们仅在PreInsertEventListener中设置了insertUserinsertTime,而没有设置updateUserupdateTime.生成的语句看起来像

The pre insert listener is called before the pre update event (if an insert as well as update happens). This happens when an entity is created, persisted and then modified in the same transaction. This will result into two seperate sql statements, first will be an insert statement and second one will be an update statement, on the same entity. With the insert statement as we set only the insertUser and insertTime in our PreInsertEventListener and not updateUser and updateTime. The generated statement will look like

insert into entity (id, .... , insert_user, insert_time, update_user, update_time) values (1, .... 'test', '21.11.2010 16:10:00', null, null)

使用PreUpdateEventListener生成的更新SQL将类似于

with the PreUpdateEventListener the update SQL generated will be like

update entity set id=1 .... , insert_user=null, insert_time=null, update_user='test', update_time='21.11.2010 16:10:00'

这篇关于使用Hibernate PreInsertEventListener更改实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆