Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

hibernate 1:M relationships & cascade

 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've run into a problem with hibernate relationships.
It only appears to occur when using a 1-to-many, bi-directional relationship with *cascade*.
Tried posting this question to the hibernate forum, but was bitterly disappointed by their attitude (admittedly they have a right to it, as they're not being payed).
Would any of you veteran hibernate users happen to find the time to try it out ? If so, i'd be forever in your debt...
-----------------------------------------
Problem in short: when using 1:M, bi-directional relationship with "cascade", hibernate saves the parent but fails to save children.
I shall demonstrate for relationship "Department has several Employees".
-----------------------------------------
The classes:
------------------------

-----------------------------------------
The mapping (bi-di relashionship, with "inverse=true" and "cascade="all":
------------------------

------------------------
Oracle tables:
------------------------

------------------------
Usage:
------------------------

------------------------
The problem:
------------------------
Department is saved to the DB, but child employees are not. According to the log, hibernate issues an sql "update" instead of "insert".
Interesting notes:
1) Problem only occurs with "cascade". When removing the cascade, you need to explicitly save each employee, but at least it works.
2) Behaviour differs if you save the new department before adding employees, namely:
session.save(dept); dept.addEmployee(emp); session.save(emp);
Again, any help would be greatly appreciated.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sol,
I noticed your display name features an initial for a last name, which goes against our naming policy. Would you mind expanding it into a full last name? You can do that via My Profile.
Thanks.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are seeing updates instead of inserts it sounds like there might be a problem with the id of the object (i.e. Hibernate has a reference to an existing object, rather than a new one). You should be able to see what values Hibernate thinks its got with debug turned on. Check these. I suggest this because I notice you are overriding equals and hashCode - something I'm not strictly sure you need to do - what's your reasoning here?
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for having a look.
1)The equals/hash are not necessary, I get the same behaviour when removing them.
2)Unsurprisingly, the log says "saveOrUpdate() previously saved instance with id: e1" , which is why it chooses to run an "update" rather than "insert" on those employees.
3)The fact those employees are marked as "already saved" appears to be somethow related to the fact it's been added to department (even though department itself was not saved yet). If you don't add them to department they get saved fine:
dept = new Department("d1");
emp=new Employee("e1", 9000);
// dept.addEmployee(emp); // If you rem this out, emp gets saved.
session.save(dept);
session.save(emp);
Thanx again.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx - your tip on the log & id's enabled me to send more focused info to the hibernate forum. Their reply indicates that the problem is with the "assigned" id. If I understand correctly (i could be wrong), they seem to assume that if an object has an id (different from 'null' or from the 'unsaved-value'), then it's an existing DB record. Which means if you manually assign the id before inserting, then hibernate would get confused. They suggested to add a "version" mapping, and it helped.
Still, seems like an odd feature to me. I would have expected hibernate to rely on some flag, separate from the id value...

Thanx again
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

is there a reason you want to assign the ids by yourself ?
if no, just let hibernate take care of them:



(there are also generators for string ids)


karl
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for relying.
Assinged IDs are required since we're using a legacy system which relies on natural keys (e.g. people stored by social security number, equipment items stored by manufacturer-issued id's, etc).
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found an FAQ entry that has some good info on this topic:

http://www.hibernate.org/116.html#A12
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sol Mam-Orn,


I'm facing the same problem. Did you manage to solve it? If so, please throw some light.

Many thanks in advance

Regards,
Bala
 
No, tomorrow we rule the world! With this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!