Change entity using Hibernate PreInsertEventListener

后端 未结 3 1187
暖寄归人
暖寄归人 2021-01-12 05:54

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.blog

相关标签:
3条回答
  • 2021-01-12 06:22

    Use the saveorupdate event listener instead.

    0 讨论(0)
  • 2021-01-12 06:24

    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.

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

    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.

    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)
    

    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'
    
    0 讨论(0)
  • 2021-01-12 06:49

    The event listeners themselves have a return value. What this return value does is tell NHibernate whether to execute the actual insert/update SQL queries. When you return false, it executes them. When you return true, it does not. This way you can suppress the actual persistence to the database.

    add a return false

    0 讨论(0)
提交回复
热议问题