my dog learned polymorphism
The moose likes Object Relational Mapping and the fly likes Hibernate in case of cascade(All,merge,persist) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate in case of cascade(All,merge,persist)" Watch "Hibernate in case of cascade(All,merge,persist)" New topic

Hibernate in case of cascade(All,merge,persist)

Pallu Sagar

Joined: Dec 04, 2009
Posts: 16

I am using two way association... i.e. oneTomany and manyToone...
Case 1 > On oneTomany mapping I had set cascade=cascadeType.persist and cascade=cascadeType.merge...
When I tried to save parent object which also includes child's set with session.persist only parent object got persist not child collection.
Is this a how hibernate works? or this operation should persist child as well.
Case 2> On oneTomany mapping I had set cascade=cascadeType.All it persist both parent as well as child collection....

Please light on this operation of hibernate in both situations...

xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 136
Sagar, i have tried using manytoone mapping and i am able to persist both entities for both conditions :- CascadeType.PERSIST and CascadeType.All

public class Employee {
private Department department;
I did not get any issue here.

below is the sample code, which i have used.

Employee emp = new Employee();
Department dept = new Department();

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17271

Where is your mappedBy. You have to tell hibernate that the two associations represent the exact same referential reference in the database. To Hibernate at this point thinks that they are two different fk to pk mappings.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 136
I donot think so mapped by is mandatory here as i am not using bi-directional relationship.
I agree. Here's the link:
subject: Hibernate in case of cascade(All,merge,persist)
It's not a secret anymore!