开发者

how to disable generator when db trigger creates id

开发者 https://www.devze.com 2023-03-05 04:37 出处:网络
Currently when I try to insert new records I am getting an error: [ERROR] 05/12/11_09:44:20.54 [org.hiber开发者_如何学JAVAnate.event.def.AbstractFlushingEventListener] - Could not synchronize databas

Currently when I try to insert new records I am getting an error:

[ERROR] 05/12/11_09:44:20.54 [org.hiber开发者_如何学JAVAnate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session

Db2 triggers to generate the ID need to remain in place to support legacy applications. How can I configure the hbm.xml to not generate the ID?


I'm not sure what version of Hibernate you are using, but Hibernate currently supports getting an ID that is generated from trigger via a special generator called select.

In short, you can add this generator to your ID column, and then reference a natural key you can use to retrieve the trigger generated ID as follows:

<id name="id" type="long" column="person_id">
  <generator class="select">
    <param name="key">socialSecurityNumber</param>
  </generator>
</id>

If your mapping already has a natural-key entry defined, then you shouldn't even need to specify the key param to the generator.

One problem with this particular generator is that you can only use one entity property as the selection key for it. If you need to select via a composite key, then you'll have to create your own generator for this purpose.

You could extend org.hibernate.id.SelectGenerator or one of it's parents, and then implement the select via multiple columns that way. Then you simply replace the class attribute of the above generator entry with the fully qualified class name of your new generator.

0

精彩评论

暂无评论...
验证码 换一张
取 消