Delete with Cascade


Let’s perform Delete operation with and without cascade to understand it better

Delete with Cascade

Annotation configuration

Applicant.java

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

@Cascade(CascadeType.DELETE)

    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="delete">
            <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="delete">
            <key>
                <column name="Applicant_Id" not-null="true" />
            </key>
            <one-to-many class="Address" />
        </set>

Example to Delete Applicant and Address objects

1
2
3
    Applicant applicant = session.get(Applicant.class, 1);
 
    session.delete(applicant);
	Applicant applicant = session.get(Applicant.class, 1);

	session.delete(applicant);


Output Queries generated


delete 

    from

        Address 

    where
        Address_Id=?


Hibernate: 

    delete 

    from

        Address 

    where
        Address_Id=?


Hibernate: 

    delete 

    from

        Applicant 

    where
        Applicant_Id=?


We can see that delete queries for Address are also executed even though we have deleted only Applicant which means Cascade has happened from Applicant to Address.

Output in DB

cascade-delete

Delete without Cascade

If we want to delete both Applicant and its reference Address , we need to delete them individually but first we need to delete Address and then we need to delete Applicant.

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 delete Applicant and Address objects

1
2
3
4
5
6
7
    Applicant applicantDelete = session.get(Applicant.class, 1);
 
        for (Address address : applicantDelete.getAddresses()) {
 
            session.delete(address);
        }
        session.delete(applicantDelete);
	Applicant applicantDelete = session.get(Applicant.class, 1);

		for (Address address : applicantDelete.getAddresses()) {

			session.delete(address);
		}
		session.delete(applicantDelete);


Output Queries generated


delete 

    from

        Address 

    where
        Address_Id=?


Hibernate: 

    delete 

    from

        Address 

    where
        Address_Id=?


Hibernate: 

    delete 

    from

        Applicant 

    where
        Applicant_Id=? 


We can see that delete queries for Address are executed first as we explictely called delete on Address object using the above code and then deleted Applicant object.

Output in DB

cascade-delete

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