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

  1. @OneToMany(mappedBy="applicant")
  2.  
  3. @Cascade(CascadeType.SAVE_UPDATE)
  4.  
  5.     private Set<Address> addresses;
@OneToMany(mappedBy="applicant")

@Cascade(CascadeType.SAVE_UPDATE)

    private Set<Address> addresses;

XML configuration

applicant.hbm.xml

  1. <set name="addresses" table="Address"
  2.                 inverse="true" lazy="true" fetch="select" cascade="save-update">
  3.             <key>
  4.                 <column name="Applicant_Id" not-null="true" />
  5.             </key>
  6.             <one-to-many class="Address" />
  7.         </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

  1. Applicant applicant = new Applicant();
  2.         applicant.setFirstName("John");
  3.         applicant.setLastName("KC");
  4.         applicant.setAge(28);
  5.         applicant.setEducation("Graduation");
  6.  
  7.         Address currentAdd = new Address();
  8.         currentAdd.setStreet("Royal road");
  9.         currentAdd.setCity("Newyork");
  10.         currentAdd.setZipcode("10001");
  11.        
  12.        
  13.         Address permanentAdd = new Address();
  14.         permanentAdd.setStreet("Manyar Road");
  15.         permanentAdd.setCity("Sydney");
  16.         permanentAdd.setZipcode("2060");
  17.        
  18.         Set<Address> addresses = new HashSet<Address>();
  19.         addresses.add(currentAdd);
  20.         addresses.add(permanentAdd);
  21.         applicant.setAddresses(addresses);
  22.         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

cascade-save-update


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

  1. @OneToMany(mappedBy="applicant")
  2.  
  3.     private Set<Address> addresses;
@OneToMany(mappedBy="applicant")

    private Set<Address> addresses;

XML configuration

applicant.hbm.xml

  1. <set name="addresses" table="Address"
  2.                 inverse="true" lazy="true" fetch="select">
  3.             <key>
  4.                 <column name="Applicant_Id" not-null="true" />
  5.             </key>
  6.             <one-to-many class="Address" />
  7.         </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

  1.         Applicant applicant = new Applicant();
  2.         applicant.setFirstName("John");
  3.         applicant.setLastName("KC");
  4.         applicant.setAge(28);
  5.         applicant.setEducation("Graduation");
  6.  
  7.         Address currentAdd = new Address();
  8.         currentAdd.setStreet("Royal road");
  9.         currentAdd.setCity("Newyork");
  10.         currentAdd.setZipcode("10001");
  11.        
  12.        
  13.         Address permanentAdd = new Address();
  14.         permanentAdd.setStreet("Manyar Road");
  15.         permanentAdd.setCity("Sydney");
  16.         permanentAdd.setZipcode("2060");
  17.        
  18.         Set<Address> addresses = new HashSet<Address>();
  19.         addresses.add(currentAdd);
  20.         addresses.add(permanentAdd);
  21.         applicant.setAddresses(addresses);
  22.         session.save(applicant);
  23.                 session.save(currentAdd);
  24.         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

cascade-save-update

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on