Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate Cascade Options – Cascade Attribute In Hibernate

Hibernate » On Aug 1, 2011 { 22 Comments } By Sivateja

Cascade Attribute In Hibernate

Main concept of hibernate relations is to getting the relation between parent and child class objects

Cascade attribute is mandatory, when ever we apply relationship between objects, cascade attribute transfers operations done on one object onto its related child objects

If we write cascade = “all” then changes at parent class object will be effected to child class object too,  if we write cascade = “all” then all operations like insert, delete, update at parent object will be effected to child object also

Example: if we apply insert(or update or delete) operation on parent class object, then child class objects will also be stored into the database.

default value of cascade =”none” means no operations will be transfers to the child class

Example: if we apply insert(or update or delete) operation on parent class object, then child class objects will not be effected, if cascade = “none”

Cascade having the values…….

  • none (default)
  • save
  • update
  • save-update
  • delete
  • all
  • all-delete-orphan

In hibernate relations, if we load one parent object from the database then child objects related to that parent object will be loaded into one collection right (see one-to-many insert example).

Now if we delete one child object from that collection, then the relationship between the parent object and that child object will be removed, but the record (object) in the database will remains at it is, so if we load the same parent object again then this deleted child will not be loaded [ but it will be available on the database ]

so finally what am saying is all-delete-orphan means, breaking relation between objects not deleting the objects from the database, hope you got what am saying ;)

Note:
what is orphan record ..?
an orphan record means it is a record in child table but it doesn’t have association with its parent in the application.

[ And ]

In an application, if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan”

And that’s it about this cascade attribute in hibernate, hope i explained all the values..!!

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

22 Responses to “Hibernate Cascade Options – Cascade Attribute In Hibernate”
  1. hi java4s team ,
    this is raju kapadne ,i got the role of Cascade Attribute In Hibernate . thanks a lot .

  2. Boss you r doing really great things

  3. Madhu says:

    In an application, if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan”

    in above statement u mentioned if we want to remove that child record immediately from the database,then we need to set the cascade =”all-delete-orphan” but u only told that all-delete-orphan means, breaking relation between objects not deleting the objects from the database, hope you got what am saying . can u explain it

  4. Pankaj says:

    Hi Sivateja
    thanks i got cascade role of relation .but can u give me more detail about save,update,delete .

  5. Ravindra says:

    what is difference between set and bag in hibernate?.that means when we are going to use set and bag?.

  6. gopi says:

    hi plz explain about lazy and proxy.i have failed in 3 interviews due to this question .so pls explain clearly . i waiting for rplyyy…

  7. P Ravi says:

    Thanks for Awesome Materiaal

  8. narendra says:

    boss
    in a spring section develop more aop applications

  9. het says:

    Perfectly explained. cleared all my doubts.Thanks a lot

  10. biplab says:

    In an application, if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan”

    in above statement u mentioned if we want to remove that child record immediately from the database,then we need to set the cascade =”all-delete-orphan” but u only told that all-delete-orphan means, breaking relation between objects not deleting the objects from the database, hope you got what am saying . can u explain it.already madhu has asked the same qs ,but there is no ans i found

  11. Rahul says:

    “You are saying is all-delete-orphan means, breaking relation between objects not deleting the objects from the database”
    and in the last line you are saying that “In an application, if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan””
    I think both are to opposite each other.
    Can u please explain it.

  12. girija says:

    Hi siva thanks for explaining

  13. Prince R says:

    Thanks for explantion.It helps me a lot

  14. yogesh says:

    Please answer Madhu’s question

    “in above statement u mentioned if we want to remove that child record immediately from the database,then we need to set the cascade =”all-delete-orphan” but u only told that all-delete-orphan means, breaking relation between objects not deleting the objects from the database, hope you got what am saying . can u explain it”

  15. Sharma says:

    Difference between Lazy and Proxy

    Lazy Initialization – you get it, only when you use it. For ex – If i want to get data only for particular user then get that data alone. And Don’t load the whole object.

    Proxy Class – Hibenrate internally uses proxy class to pull your data when you use get() method. proxy class is sub class of your user class.

    Hope this helps.

  16. Somrita says:

    What is inverse? what is the difference between cascade and inverse?

  17. srinivas s says:

    thank you java4s . i got full about cascade

  18. Murali says:

    Hi Sivateja…!
    While I am performing delete operation by using cascade=”all”, i deleted parent obj…then child objs which are associated with parent objs also deleted from the database table also…ok fine, while using cascade=”all-delete-orphan” , same effects i have seen in the database side…i didnt find any difference..please could you explain me to murali.tekumatla@gmail.com

    any one can please explain me to my mail….

  19. Vikrant says:

    Please give the real time example of cascade=”all”

  20. pradeep mishra says:

    really to made to understand what cascade is but u missed merge

  21. Venkat says:

    Thanking u java4s and this explanation easily understand

  22. namo says:

    nice tutorial…………….

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.