"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 department and employee example. Multiple employees corresponding to a department (many-to-one association 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 ">  
Can see, in the Set tab above. We define the cascade= "Save-update" attribute, which, when the session is saved or updated by the Save (), update (), Saveorupdate () method, Cascade saves all the newly created temporary objects associated with the object. And the 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 ();   }          }      }  }
Can see that we have defined two methods: Add () and delete ()

Add (): In this method. We saved only the Department object when we saved it. However, it is based on the cascade= "Save-update" attribute. Two employee objects are also saved in the database

Delete (): In this approach, we want to delete the data in the employee table associated with the Department table in the database by directly deleting the Department object. But. The cascade we configured is to require a cascade relationship when saving or updating, so let's say that after we run the Delete () method, only the data in the department table is deleted. The foreign key of the employee table's data becomes null. But the data won't be erased.


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

The values of the Cascade property are:

1. None: Ignores other associated objects. The default value.



2. Save-update: When the session is saved or updated by the Save (), update (), Saveorupdate () method, the Cascade saves all the newly created temporary objects associated with the object. Also, cascade updates all associated free objects.



3, persist: When the session through the persist () method to save the current object. The newly created temporary objects that are all associated with the Cascade are saved.

4. Merge: When the current object is saved by the merge () method of the session. Cascade dissolve all associated free objects.

5. Delete: When the current object is deleted by Delete (), all associated objects are cascaded.

6. Lock: Adds the current free object to the session cache by lock (). The entire free object will also be 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: Refreshes all associated objects when the current object is refreshed with refresh ().

(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, delete all and current objects. An object that unlocks the behavior of the association.



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





"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.