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

Posted by Leonardo on Stack Overflow See other posts from Stack Overflow or by Leonardo
Published on 2010-11-09T13:12:13Z Indexed on 2011/03/12 8:10 UTC
Read the original article Hit count: 256

Hi All,

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

thanks

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about foreign-key-relationship