开发者

Hibernate not saving foreign key, but with junit it's ok

I have this strange problem. In a J2ee webapp with spring, smartgwt and hibernate, it happens that I have a class A wich has a set of class B, both of them mapped to table A and table B.

I wrote a simple test case for testing the service manager which is supposed to do insert, update, delete and everything work as expected especially during insert. In the end I have one record in A and records in B with foreign key to A.

But when I try to call the service from the web app, the entity in B are saved without a foreign key reference.

I am sure that the service is the same. One thing I noticed is that enabling hibernate logging, seems that when the service is called from the application, one more update is made:

  • insert A
  • insert B
  • update A
  • update B
  • update B (foreign key only)
  • update A <--- ???
  • update B <--- ???

Instead, when junit t开发者_JAVA百科est case is run, the update is as follows:

  • insert A
  • insert B
  • update A
  • update B
  • update B (foreign key only)

I suppose the latest update is what is causing the erroe, maybe it is overwriting values. Considering that the app is using spring, with the well known mechanism of DAO + Manager, where can I investigate to solve this issue ? Someone told me that the session is not closed, so hibernate would do one more update before release the objects by itself.

I am pretty sure that all the configuration hbm, xml, and the rest are fine...but I maybe wrong.


Just guessing: could it be that you use the objects after you inserted them? Hibernate stores the actual memory state at point in time when you call commit. So if you call your service which inserts the objects (calling session.save), but then, in the calling code you change the entities again, you force Hibernate to do another update to store your latest changes. Check the objects in memory at the end of the transaction.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜