English 中文(简体)
保存或更新后使用hibernate侦听器
原标题:using hibernate listener after saveorupdate

在我的项目中,一些实体应该具有基于其值的签名属性。例如,在创建实体并填充其属性后,当调用entity.saveorupdate()方法时,hibernate事件侦听器应该控制并从db收集uniqueid的下一个序列值。那么同一个事件应该基于实体属性和这个uniqueid生成一个签名(比如String)。签名之后,应该调用真正的saveorupdate事件。所以填充了符号属性的实体被写入数据库。

问题是,在saveorupdate事件中,我做了一些工作并调用了saveorupdate,所以会发生无休止的操作。

你对这类问题有什么解决办法吗?(而不是使用面向方面的编程)

问题回答

一个快速而肮脏的解决方案是使用ThreadLocal来保存实体。如果threadlocal为空,则更新签名,设置threadlocal,然后保存实体。在下一次调用中,threadlocal将被填充,您可以从侦听器返回。

您可以做的另一件事是在实际saveOrUpdate之前应用侦听器,如果没有ID,则跳过。并在创建实体时手动调用逻辑(每个后续更新都将由侦听器处理)





相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签