Premise:
Inverse: Responsible for foreign key maintenance;
Cascade: Responsible for adding and deleting changes to records;
Basis:
The "inverse" property in Hibernate has only two values of "true" and "false". "True" indicates that the power to maintain the relationship is given to the other party, "false" means that the maintenance power is not surrendered (the default value).
Hibernate "Cascade"-the written explanation for "This property allows us to manipulate the main object while Hibernate helps us complete the corresponding operation of the subordinate object".
* none--do not use cascading in all cases, this is the default value
* save-update--cascade Save or update [correlate operation when executing save/update/saveorupdate]
* delete--Cascade Delete
* delete-orphan--orphan deleted. (Note: only applies to one-to-many relationships)
#* all--except for Delete-orphan. (includes save-update + delete)
* all-delete-orphan--contains all cases of Delete-orphan. (includes save-update delete Delete-orphan)
Where to appear:
Inverse: can only be set in Set, list, map and several other tags, such as many-to-one, such as the label can not set the "inverse" this property value
Cascade: One or more parties are allowed.
Experience:
In development, the most common is a one-to-many relationship < many-to-many will be split into two one-to-many, and most of these two properties are configured in one side of the mapping file [<set name= "Linkmans" inverse= "true" cascade= "all" Lazy...fetch.;]
Code Show:
Customer and Linkman one-to-many
---
public class Customer {private Long cust_id;private String cust_name;...private set<linkman> linkmans = new hashset& Lt Linkman> (); Getter (); setter (); ...}
mapping files
-------
public class Linkman {private Long lkm_id;private String lkm_name;...private Customer customer;getter (); setter (); }
mapping files
----JUnit Test Code
@Testpublic void Run () {Session session = Hibernateutils.getsession (); Transaction tr = session.begintransaction (); Customer C1=new customer (); C1.setcust_name ("Jack"); Linkman l1=new Linkman (); L1.setlkm_name ("Spider-man"); C1.getlinkmans (). Add (L1); Session.saveorupdate (L1); Tr.commit ();}
- Result one party record multiparty record save multiparty foreign key is empty
- A rough explanation:
The saving of records in the table is only related to cascade, and the updating of the values of the foreign key fields is only related to the inverse; configuring Cascade= "All" on one side means that the save side is cascaded
Saving records < foreign key updates in a multiparty table depends on inverse>,inverse= "true" means that one party abandons the foreign key maintenance, and the foreign key maintenance rights are handed over to many parties, so the new
The value of the foreign key is empty in the record. Add code [L1.setcustomer (c1);] The foreign key has a value.
- The same test code, other cases Discrimination:< No add L1.setcustomer (C1);>
- One side inverse=false cascade=all------result one Party records multiple records foreign key all save
- One party inverse=true multiparty cascade=all------result one party record saved
When you save a multi-party object
@Testpublic void Run2 () {Session session = Hibernateutils.getsession (); Transaction tr = session.begintransaction (); Customer C1=new customer (); C1.setcust_name ("Jack"); Linkman l1=new Linkman (); L1.setlkm_name ("Spider-man"); Session.saveorupdate (L1); Tr.commit ();}
---Party inverse=true multiparty cascade=all------result multiparty record save foreign key is empty
@Testpublic void Run2 () {Session session = Hibernateutils.getsession (); Transaction tr = session.begintransaction (); Customer C1=new customer (); C1.setcust_name ("Jack"); Linkman l1=new Linkman (); L1.setlkm_name ("Spider-man"); L1.setcustomer (C1); session.saveorupdate (L1); Tr.commit ();}
---Party inverse=true multiparty cascade=all------Results one party records multiple records foreign key all save
For many-to-many < on the stack overflow explanation of the excerpt;:
In case of many-to-many relation through intermediary table; "Cascade" says whether a record would be created/updated in the child table. Whereas "inverse" says whether a record would be created/updated in the intermediary table.
[Note]: personal opinion, improper place welcome correction.
The interpretation of Hibernate inverse and cascade