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
2
3
4
5
@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

1
2
3
4
5
6
7
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

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
2
3
@OneToMany(mappedBy="applicant")
 
    private Set<Address> addresses;
@OneToMany(mappedBy="applicant")

    private Set<Address> addresses;

XML configuration

applicant.hbm.xml

1
2
3
4
5
6
7
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        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

cascade-save-update

About the Author

Karibasappa G C (KB)
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