save-update with Cascade
Let’s perform Save-Update operations with and without cascade to understand it better
save-update with Cascade
Annotation configuration
Applicant.java
- @OneToMany(mappedBy="applicant")
- @Cascade(CascadeType.SAVE_UPDATE)
- private Set<Address> addresses;
@OneToMany(mappedBy="applicant") @Cascade(CascadeType.SAVE_UPDATE) private Set<Address> addresses;
XML configuration
applicant.hbm.xml
- <set name="addresses" table="Address"
- inverse="true" lazy="true" fetch="select" cascade="save-update">
- <key>
- <column name="Applicant_Id" not-null="true" />
- </key>
- <one-to-many class="Address" />
- </set>
<set name="addresses" table="Address" inverse="true" lazy="true" fetch="select" cascade="save-update"> <key> <column name="Applicant_Id" not-null="true" /> </key> <one-to-many class="Address" /> </set>
Example to persist Applicant and Address objects
- Applicant applicant = new Applicant();
- applicant.setFirstName("John");
- applicant.setLastName("KC");
- applicant.setAge(28);
- applicant.setEducation("Graduation");
- Address currentAdd = new Address();
- currentAdd.setStreet("Royal road");
- currentAdd.setCity("Newyork");
- currentAdd.setZipcode("10001");
- Address permanentAdd = new Address();
- permanentAdd.setStreet("Manyar Road");
- permanentAdd.setCity("Sydney");
- permanentAdd.setZipcode("2060");
- Set<Address> addresses = new HashSet<Address>();
- addresses.add(currentAdd);
- addresses.add(permanentAdd);
- applicant.setAddresses(addresses);
- session.save(applicant);
Applicant applicant = new Applicant(); applicant.setFirstName("John"); applicant.setLastName("KC"); applicant.setAge(28); applicant.setEducation("Graduation"); Address currentAdd = new Address(); currentAdd.setStreet("Royal road"); currentAdd.setCity("Newyork"); currentAdd.setZipcode("10001"); Address permanentAdd = new Address(); permanentAdd.setStreet("Manyar Road"); permanentAdd.setCity("Sydney"); permanentAdd.setZipcode("2060"); Set<Address> addresses = new HashSet<Address>(); addresses.add(currentAdd); addresses.add(permanentAdd); applicant.setAddresses(addresses); session.save(applicant);
We can observe that, we are saving only Applicant object in the above code but Address objects will also be saved automatically due to Cascade.
Output Queries generated
insert into Applicant (Age, Education, FirstName, LastName, Applicant_Id) values (?, ?, ?, ?, ?) insert into Address (Applicant_Id, City, Street, Zipcode, Address_Id) values (?, ?, ?, ?, ?) insert into Address (Applicant_Id, City, Street, Zipcode, Address_Id) values (?, ?, ?, ?, ?)
We can see that insert queries for Address are also executed even though we saved only Applicant which means Cascade has happened from Applicant to Address.
Output in DB
save-update without Cascade
If we want to save both Applicant and its reference Address without using Cascade , we need to save them individually
Annotation configuration
Applicant.java
- @OneToMany(mappedBy="applicant")
- private Set<Address> addresses;
@OneToMany(mappedBy="applicant") private Set<Address> addresses;
XML configuration
applicant.hbm.xml
- <set name="addresses" table="Address"
- inverse="true" lazy="true" fetch="select">
- <key>
- <column name="Applicant_Id" not-null="true" />
- </key>
- <one-to-many class="Address" />
- </set>
<set name="addresses" table="Address" inverse="true" lazy="true" fetch="select"> <key> <column name="Applicant_Id" not-null="true" /> </key> <one-to-many class="Address" /> </set>
Example to persist Applicant and Address objects
- Applicant applicant = new Applicant();
- applicant.setFirstName("John");
- applicant.setLastName("KC");
- applicant.setAge(28);
- applicant.setEducation("Graduation");
- Address currentAdd = new Address();
- currentAdd.setStreet("Royal road");
- currentAdd.setCity("Newyork");
- currentAdd.setZipcode("10001");
- Address permanentAdd = new Address();
- permanentAdd.setStreet("Manyar Road");
- permanentAdd.setCity("Sydney");
- permanentAdd.setZipcode("2060");
- Set<Address> addresses = new HashSet<Address>();
- addresses.add(currentAdd);
- addresses.add(permanentAdd);
- applicant.setAddresses(addresses);
- session.save(applicant);
- session.save(currentAdd);
- session.save(permanentAdd);
Applicant applicant = new Applicant(); applicant.setFirstName("John"); applicant.setLastName("KC"); applicant.setAge(28); applicant.setEducation("Graduation"); Address currentAdd = new Address(); currentAdd.setStreet("Royal road"); currentAdd.setCity("Newyork"); currentAdd.setZipcode("10001"); Address permanentAdd = new Address(); permanentAdd.setStreet("Manyar Road"); permanentAdd.setCity("Sydney"); permanentAdd.setZipcode("2060"); Set<Address> addresses = new HashSet<Address>(); addresses.add(currentAdd); addresses.add(permanentAdd); applicant.setAddresses(addresses); session.save(applicant); session.save(currentAdd); session.save(permanentAdd);
In the above code, we are using session.save(currentAdd) and session.save(permanentAdd) to save the address objects.
and we are using session.save(applicant) to save the Applicant object.
Output Queries generated
insert into Applicant (Age, Education, FirstName, LastName, Applicant_Id) values (?, ?, ?, ?, ?) insert into Address (Applicant_Id, City, Street, Zipcode, Address_Id) values (?, ?, ?, ?, ?) insert into Address (Applicant_Id, City, Street, Zipcode, Address_Id) values (?, ?, ?, ?, ?)
We can see that insert queries for Address are also executed as we saved them explicitly in the above code using session.save(currentAdd) and session.save(permanentAdd)
” If we use Cascade then no need to write this additional code to save the references. “
Output in DB