Hibernate "Inverse and cascade attributes" knowledge essentials

Source: Internet
Author: User

Inverse property

Inverse Property: Indicates whether control is transferred .

    • True: Control has been transferred "the current party has no control"
    • False: Control is not transferred "the current party has control"

The inverse attribute is useful when maintaining an association relationship . This attribute can only be used on one side! The default value for the inverse property is Fasle, which means that the current party has control over the

Take a look at some aspects of how inverse works when maintaining association relationships :

    • Save data
    • Get Data
    • Disassociate relationship
    • Effects of deleting data on association relationships
Save data

Set the inverse property to Ture so that dept has no control

        <!--维护关系的是Set集合,对应employee表-->        <set  cascade="save-update" name="set" table="employee" inverse="true">

When you perform a save data operation, you find that hibernate only executes three SQL statements. The foreign key of the employee table has no data

Conclusion: If the control inversion is set, that is inverse=true, then the association relationship is maintained through the department side. While saving the department, the data is saved, but the relationship is not maintained. The foreign key field is null

Querying data

Set the inverse property to True, then the department side has no control.

        <set  cascade="save-update" name="set" table="employee" inverse="true">

When we looked at the data, we found that the inverse attribute had no effect on the query data.

        //查询出Dept对象        Dept de = (Dept) session.get(Dept.class1);        System.out.println(dept.getDeptName());        System.out.println("-----------");        //用到的时候再查询数据库,Hibernate的懒加载【后面会讲解】        System.out.println(de.getSet());

Disassociate relationship

Departments and employees are connected by foreign keys, so let's see if the inverse property has any effect on disassociate relationships .

    • The inverse property is false when the department side has permissions
        //查找部门id为1的信息        Dept dept1 = (Dept) session.get(Dept.class1);        //清除关联信息        dept1.getSet().clear();

You can disassociate the relationship, and the foreign key field of the employee is set to null.

    • The inverse property is true when the department side does not have permissions
        //查找部门id为2的信息        Dept dept1 = (Dept) session.get(Dept.class2);        //清除关联信息        dept1.getSet().clear();

Cannot disassociate relationship

Effects of deleting data on association relationships
    • The inverse property is false when the department side has permissions
        //查找部门id为2的信息        Dept dept1 = (Dept) session.get(Dept.class2);        //删除部门2        session.delete(dept1);        

When there is control, you can delete the data, first set the foreign key to NULL, and then delete the data!

    • The inverse property is true when the department side does not have permissions
        //查找部门id为1的信息        Dept dept1 = (Dept) session.get(Dept.class1);        //删除部门1        session.delete(dept1);        

Throws the exception directly, says that the department has the foreign key, cannot delete the data!

Cascade Property

Cascade the meaning of cascading, which is simply the effect on other associated fields when manipulating a property

The Casecade property does not resemble the inverse property only in the "one" side of the set, it can be on the "one" side can also be set on the "many" side

Cascade has so many values

    • None cascade operation, default value
    • Save-update Cascade Save or update
    • Delete Cascade deletion
    • Save-update,delete Cascade Save, UPDATE, delete
    • All ibid. Cascade Save, UPDATE, delete
Cascade Save

What is the use of the level of UNPROFOR??? Let's look at an example:

    • An error occurs when you save the object without saving the associated object in the database.
    • Because it will find that dept is a foreign key, and the key is another object to save, this object does not exist in the database table, and therefore throws an exception
 //Create object  Dept Dept = new  dept  (); Dept.setdeptname  ( "Development Department" ); Employee ZS = new  employee  (); Zs. setempname  ( "Zhang Shan" ); Zs. setsalary  (1111 ); Employee ls = new  employee  (); Ls. setempname  ( "John Doe" ); Ls. setsalary  (2222 ); //maintenance relationship  Dept.getset  (). add  (ZS); Dept.getset  (). add  (LS); //Save Dept Object  Session. save  (dept); 

If we set up Cascade Save in Dept, Hibernate will know: when we save Dept data, we find Dept foreign Key, and save the object of Dept foreign key in the database .

    <set name="set" table="employee" cascade="save-update">

Cascade Delete

Cascade Delete, this is too risky for us, if the deletion of some data, will be the other associated data also deleted ... In practice we generally do not use!

    • In the absence of a cascade delete, we try to delete the Dept
        //删除部门为3的记录        Dept dept1 = (Dept) session.get(dept.getClass3);        session.delete(dept1);

It will first delete 3 of the foreign key corresponding record, and then delete the department's data

    • When setting up cascade Delete, we try to delete dept
        <set name="set" table="employee" cascade="save-update,delete"  >
    • Cascading delete Data
        //删除部门为4的记录        Dept dept1 = (Dept) session.get(dept.getClass4);        session.delete(dept1);

We found no data associated with it.

Cascade and Inverse simultaneous use

Above we have introduced cascade and inverse all over again, then cascade and inverse at the same time how to use it??? Let's test it.

    • Setting dept does not have control, but setting cascade save, delete
        <set name="set" table="employee" cascade="save-update,delete" inverse="true"  >
    • Add a Dept Object

          //添加一个dept对象    session.save(dept);

If we simply set the inverse property to True, then the database must not be able to maintain the association relationship "Here we have tested"

However, the Cascade save is now also set, whether the associated relationship of the object can be saved in the database .

Let's take a look at the results:

The priority of the inverse is higher than the priority of the cascade, so if the inverse property is set to True, then cascade is invalid.

Refer to the Detailed blog:

Http://www.cnblogs.com/whgk/p/6135591.html

Ascade and Inverse Summary

The above tests are done in several ways and look a bit more, so let's summarize

Inverse property

The inverse property can be set only in the one party . Inverse=false said there was control, Inverse=ture said there was no control.

    • When you save the associated information
      • Have control---> can save corresponding associated data
      • No Control---> data is saved, but the correlation is not maintained, that is, the foreign key column is null
    • When querying the data
      • There is no control over the query data has no effect
    • When you disassociate a relationship
      • Have control over---> can disassociate relationships
      • No Control---> cannot disassociate, does not generate an UPDATE statement, and does not error
    • Impact on association relationships when data is deleted
      • Have control---> set the value of the foreign key to NULL, and then delete the data
      • No Control---> if the deleted record has been referenced by a foreign key, will error, violate the primary foreign key reference constraint, if the deleted record is not referenced, you can delete it directly .

Many-to-many relationships are the same, but many-to-many associations are in the middle table

Cascade Property

Cascade has so many values:

    • None cascade operation, default value
    • Save-update Cascade Save or update
    • Delete Cascade deletion
    • Save-update,delete Cascade Save, UPDATE, delete
    • All ibid. Cascade Save, UPDATE, delete

What we may use often is: save-update this value, because the risk of cascading deletion is too great !

    • Cascade Save
      • Cascading is not set--if you save only one object and the object has a foreign key, an exception is thrown
      • Set Cascade Save-then you can save the object and the object associated with it.
    • Cascade Delete
      • Cascade Delete is not set--when the data is deleted, the foreign key field is set to NULL, and the current party's record is deleted
      • Cascade Delete is set and the records associated with the object are deleted.

If cascade and inverse are set at the same time:

    • Inverse attribute precedence is higher than cascade, if the inverse property is set to True, then cascade is invalid!

If the article is wrong, welcome to correct, we communicate with each other. Accustomed to look at technical articles, want to get more Java resources of students, can pay attention to the public number: Java3y

Hibernate "Inverse and cascade attributes" knowledge essentials

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.