This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pallu Sagar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I donot think so mapped by is mandatory here as i am not using bi-directional relationship.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic