This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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
Author

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

Pallu Sagar
Greenhorn

Joined: Dec 04, 2009
Posts: 16
HEllo

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




-Sagar
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
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 {
...............
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="department_id")
private Department department;
}
I did not get any issue here.

below is the sample code, which i have used.

Employee emp = new Employee();
emp.setName("Sunil");
emp.setSex("M");
Department dept = new Department();
dept.setDepartmentName("HR1");
em.getTransaction().begin();
emp.setDepartment(dept);
em.persist(emp);

-Sunil
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

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.

Mark


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: 133
I donot think so mapped by is mandatory here as i am not using bi-directional relationship.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate in case of cascade(All,merge,persist)