"SSH three frames" Hibernate basics Nineth: Cascade Operations for Cascade Association relationships

Source: Internet
Author: User
Tags set set

Here is the cascade operation of Hibernate's associated relationship, which is controlled using the Cascade property.


Still use departments and employees for example, multiple employees corresponding to a department (many-to-one relationship)

Employee class: Employee.java

Package Cn.itcast.hibernate.domain;public class Employee {      private int id;      private String name;      Private Department depart;            public int getId () {          return ID;      }      public void setId (int id) {          this.id = ID;      }      Public String GetName () {          return name;      }      public void SetName (String name) {          this.name = name;      }      Public Department Getdepart () {          return depart;      }      public void Setdepart (Department depart) {          This.depart = depart;      }  }  
Employee class mapping File: Employee.hbm.xml

<?xml version= "1.0"?>  <! DOCTYPE hibernate-mapping public       "-//hibernate/hibernate mapping DTD 3.0//en"      "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">  
Department Category: Department.java

Package Cn.itcast.hibernate.domain;import Java.util.set;public class Department {      private int id;      private String name;      Private set<employee> Emps;        Public set<employee> Getemps () {return emps;} public void Setemps (set<employee> emps) {this.emps = Emps;} public int getId () {          return ID;      }      public void setId (int id) {          this.id = ID;      }      Public String GetName () {          return name;      }      public void SetName (String name) {          this.name = name;      }  }  
Departmental mapping File: Department.hbm.xml

<?xml version= "1.0"?>  <! DOCTYPE hibernate-mapping public       "-//hibernate/hibernate mapping DTD 3.0//en"      "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">  
As you can see, in the set tag above, we define the cascade= "Save-update" property, when the session saves or updates the object by using Save (), update (), Saveorupdate () method. Cascade saves all associated new temporary objects, and cascade updates all associated free objects

Let's write a test class: Many2one.java

Package Cn.itcast.hibernate;import Java.util.hashset;import Java.util.set;import org.hibernate.hibernate;import Org.hibernate.session;import Org.hibernate.transaction;import Cn.itcast.hibernate.domain.department;import Cn.itcast.hibernate.domain.employee;public class Many2one {public static void main (string[] arg) {departmen            T depart = Add ();    Delete (1);          } static Department Add () {Session s = null;          Transaction tx = NULL;                 try{Department depart = new Department ();                            Depart.setname ("Depart name");              Employee EMP1 = new Employee (); Emp1.setdepart (depart);                                      Establish an association relationship of two objects emp1.setname ("emp name");              Employee EMP2 = new Employee (); Emp2.setdepart (depart);                              Establish an association relationship of two objects emp2.setname ("emp name");            set<employee> emps = new hashset<employee> (); //Add two Employee objects to the set set Emps.add (EMP1);            Emps.add (EMP2);                        Injects set set properties Depart.setemps (Emps) for department objects;              s = hibernateutil.getsession ();                         tx = S.begintransaction ();                         Save Department Object S.save (depart);              Tx.commit ();          return depart;              }finally{if (s!=null) {s.close ();          }}} static Department delete (int departid) {Session s = null;          Transaction tx = NULL;              try{s = hibernateutil.getsession ();              tx = S.begintransaction (); Department depart = (Department) s.get (Department.class, Departid);                        Query S.delete (depart) based on ID;              Tx.commit ();          return depart;              }finally{if (s!=null) {s.close ();   }          }      }  }
You can see that we have defined two methods: Add () and delete ()

Add (): In this method, we save only the Department object, but according to the cascade= "Save-update" property, two employee objects are also saved in the database

Delete (): In this method, we want to delete the data in the employee table associated with the Department table in the database by directly deleting the Department object. However, we configure the cascade to require a cascade relationship when saving or updating, so if we execute the Delete () method, only the data in the department table will be deleted, the foreign key of the employee table's data will become null, but the data will not be deleted


Let's take a look at the value of Cascade:

The values of the Cascade property are:

1. None: Ignores other associated objects, default values.

2. Save-update: When the session saves or updates an object through Save (), update (), the Saveorupdate () method, Cascade saves all the associated new temporary objects, and cascade updates all associated free objects.

3, persist: When the session through the persist () method to save the current object, Cascade saves all the associated new temporary objects.

4. Merge: When the current object is saved through the merge () method of the session, all associated free objects are cascaded.

5. Delete: When you delete the current object through Delete (), all associated objects are cascaded.

6. Lock: When the current free object is added to the session cache by lock (), all the free objects are added to the session cache.

7. Replicate: When the current object is copied by replicate (), all associated objects are cascaded.

8, evict: When the session cache object is cleared through evict (), all associated objects are cascaded.

9. Refresh: When the current object is refreshed with refresh (), all associated objects are cascaded. (refresh refers to updating data in session cache synchronously)

10, All:save-update (), persist (), merge (), delete (), lock (), replicate (), evict () and refresh () behavior.

11, Delete-orphan, removes all and the current object when the object that disassociate the behavior.

12, All-delete-orphan; When you delete the current object through Delete (), all associated objects are cascade-deleted.





"SSH three frames" Hibernate basics Nineth: Cascade Operations for Cascade Association relationships

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.