• 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 in case of cascade(All,merge,persist)

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


 
Ranch Hand
Posts: 147
  • 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
 
ranger
Posts: 17344
11
Mac IntelliJ IDE 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: 147
  • 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.
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!