• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...


 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I donot think so mapped by is mandatory here as i am not using bi-directional relationship.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic