Inverse Function in HBM file in hibernate

Source: Internet
Author: User
4.01. Associate update
The property corresponding to "link tag" is a pojo or a pojo set. Modifying the value of "link attribute" may update the maintable table or the relationtable table.

This type of update is currently called "associated Update ".


4.1. Inverse Role of the attribute (assuming the cascade attribute is not set)
4.1.1 only set/MAP/LIST/array/bag Inverse Attribute ".
---- Take the mark set as an example. Specifically, it is "school Table of an address table" -- address. schoolset.

4.1.2 "SetInverseThe attribute determines whether the set changes are reflected in the database.
Inverse= False ---- reflection;Inverse= True ---- does not reflect"
InverseThe default attribute is false.


Applicable to the <one-to-operate> and <allow-to-operate> subtags.
4.1.2 is applicable to any set operation.

4.1.3 When Inverse = False, Hibernate How to reflect changes to the set to the database:

The operations on set mainly include: (1) adding the element address. getschoolset (). Add (oneschool );
(2) Delete the element address. getschoolset (). Remove (oneschool );
(3) Delete set address. setschoolset (null );
(4) set the new set address. setschoolset (newschoolset );
(5) transfer set otherschoolset = otheraddress. getschoolset ();
Otheraddress. setschoolset (null );
Address. setschoolset (otherschoolset );
(6) Changing the attribute value of the element in the set. If the key attribute is changed, this will cause an exception.
If a common attribute is changed Hibernate I think the set has not changed (the reason can be seen later ).
Therefore, this situation is not considered.

After changing the set, Hibernate Database Operations vary depending on the <one-to-one> relation or <others-to-minus> relation.

Changes to One-to-school and school set will change the data in the school Table:
# School_id is the primary key of the school table, and school_address is the address bar of the school table.
# The foreign key of the table school is school_address, which corresponds to the primary key address_id of the table address
(11) insert oneschool ---- sqlinsertrowstring:
Update school set school_address =? Where school_id =?
(Only update the value of foreign-key .)
(22) delete oneschool ---- sqldeleterowstring:
Update school set school_address = NULL where school_id =?
(It is strange to set foreign-Key to null. I don't know what the actual meaning is ?)
(33) delete belongs to all schools ---- sqldeletestring of an address:
Update school set school_address = NULL where school_address =?
(44) Update ---- sqlupdaterowstring: "", no need

Changes to school set will change the data in the address_school relational table:
# The relationship between "region-school" and "many to many" is a little far-fetched, just to make it easier to compare with the one-to-many relationship above.
# Assume that there is a relational table address_school, which has two fields address_id, school_id,
# These two fields correspond to the keys of the address and school tables respectively.
(11) insert SQL statement: insert into address_school (address_id, school_id)
Values (?,?)
(22) The delete SQL statement is: delete from address_school.
Where address_id =? And school_id =?
(33) The SQL statement for delete all is: delete from address_school
Where address_id =?
(44) The update SQL statement is ---- sqlupdaterowstring:
Update address_school set address_id =?
Where address_id =? And school_id =?

Operations on set (1 ), Hibernate Will Execute (11) sqlinsertrowstring
Operations on set (2 ), Hibernate Will Execute (22) sqldeleterowstring
Operations on set (3 ), Hibernate Will Execute (33) sqldeletestring
For the set operation (4), the old schoolset is deleted because it does not have the address to which it belongs, that is, the system first executes (33) sqldeletestring
Then add a new schoolset, that is, execute sqlinsertrowstring
The operation on set (5) actually transfers the set from one pojo to another:
Run sqldeletestring to delete the school to which the otheraddress belongs.
Then, run sqldeletestring to delete the original school address.
Finally, execute sqlinsertrowstring to add the otherschoolset to the address

Conclusion: (1) for one-to-one Operations, changing set will make Hibernate Execute a series of update statements and do not delete/insert data
(2) For sequence-to-sequence, changing set only modifies the data in the relational table does not affect the other side of sequence-to-sequence.
(3) Although one-to-one operation and one-to-one operation are different, the objective is to maintain data consistency. All executed SQL statements
Only the "bridge field" is involved, and other fields are not considered or changed. Therefore, the operation on set (6) is ineffective.
Extend: For list, the index field may be maintained.

4.1.4"InverseIt has nothing to do with cascade and has nothing to do with each other ."
After commit, the two attributes have different timing to play a role,HibernateBased on changes to the pojo object and cascade attribute settings,
Generates a series of actions, such as updateaction, deleteaction, and insertaction. Each action has an execute method to execute the corresponding SQL statement.
After all these actions are generated,HibernateExecute them together. Before executing SQL,InverseAttribute function,
WhenInverse= True: SQL is not executed.InverseIf it is set to false, run the SQL statement.


4.1.5 Inverse The default value is false, so Inverse By default, "association Update" is performed for properties ".

4.1.6 suggestion: Set only for set + sequence-to-Sequence Inverse = False. Other tags are not considered. Inverse Attribute.
Unfortunately, it is not set. Inverse Attribute, Inverse The default value is false.

4.2. Role of cascade attributes:
4.2.1 The cascade attribute is available only for "link tag": Allow-to-one, one-to-one, any,
Set (MAP, bag, idbag, list, array) + One-to-pair (bytes-to-Pair)

4.2.2 cascade refers to whether the associated object (passive party) performs the same operation simultaneously when the primary prosecution executes the operation.
The relationship between pojo and its link property is the relationship between the "Master Control-passive side". If the link property is a set, the passive side is one element in the set ,.
For example, a school has three attributes: region, principal, and student. The element is student)
When session. Delete (school) is executed, cascade determines whether to execute session. Delete (address), session. Delete (themaster ),
Whether to execute session. Delete (astudent) for each astudent ).

Extend: Inverse Properties are different. See section 4.3.

4.2.3 a cascade operation may trigger multiple associated operations. The previous operation is called "Master operation", and the next operation is called "associated operation ".
Optional value of the cascade attribute:
ALL: Perform Association operations in all cases.
None: No association operation is performed in all cases. This is the default value.
Save-Update: perform join operations when you execute save/update/saveorupdate.
Delete: perform join operations when executing Delete.

The specific "associated operation" is based on the "Master operation:
"Master operation" and "associated operation"
Session. saveorupdate --> session. saveorupdate (execute saveorupdate to actually execute save or update)
Session. Save ----> session. saveorupdate
Session. udpate --> session. saveorupdate
Session. Delete --> session. Delete

4.2.4 the sequence of the master and associated operations is "save one first, then save the snapshot; Delete the snapshot first, then delete one; update the master prosecution first, and then update the passive party"
For one-to-one, when its attribute constrained = "false" (default), it can be considered as a one-to-one relationship;
When its attribute constrained = "true", it can be seen as a forward-to-one relationship;
For role-to-role, it can be considered as one-to--role.

For example, a school has three attributes: Region (address), Principal (themaster, whose constrained = "false"), and student (set, whose element is student)
When session. Save (school) is executed,
The actual execution order is: Session. Save (Address );
Session. Save (school );
Session. Save (themaster );
For (for every student ){
Session. Save (astudent );
}

When session. Delete (school) is executed,
The actual execution sequence is: Session. Delete (themaster );
For (for every student ){
Session. Delete (astudent );
}
Session. Delete (school );
Session. Delete (Address );

When session. Update (school) is executed,
The actual execution sequence is: Session. Update (school );
Session. saveorupdate (Address );
Session. saveorupdate (themaster );
For (for every student ){
Session. saveorupdate (astudent );
}
Note: The Association operation caused by cascade operations is the saveorupdate operation, rather than the update operation.
The difference between saveorupdate and update is that the former decides whether to execute update or save based on whether the operation object is saved or not saved.

Extends: In reality, deleting a school will not delete the region, that is, the cascade of the region is generally set to false.
In addition, cascade = true is rarely set in the sequence-to-sequence relationship, but is set Inverse = False. This reflects cascade and Inverse . See 4.3

4.2.6 The default value of cascade is false, so Inverse By default, "association Update" is performed for properties ".

4.2.7 conclusion: cascade also performs this operation on its attributes (its cascade = true) when an object is operated.

4.3 Inverse Comparison with cascade
These two attributes do not affect each other, but they have a similar effect, which can lead to updates to the relational table.

4.3.1 Inverse It is only valid for set + One-to-minus (or operation-to-minus), and is invalid for operation-to-one and one-to-one.
Cascade is valid for all link tags.

4.3.2 Inverse It takes effect for the whole set object. Cascade takes effect for one element in the Set object. If the set is empty, cascade will not trigger association operations.
For example, set the set object to null, school. setstudentset (null)
Inverse Cause Hibernate Run: udpate student set school_id = NULL where school_id =?
Cascade does not perform join update on the student table because there are no elements in the set.

Add another school, session. Save (school)
Inverse Cause Hibernate Run:
For (for every student in school ){
Udpate student set school_id =? Where student_id =? // Change the student's school_id to the new school ID
}
Caused by Cascade Hibernate Run:
For (for every student in school ){
Session. Save (astudent); // execute the save operation on the student
}

Extends: If you change some elements in a set (for example, adding an element ),
Inverse : Hibernate First, determine which elements have changed and execute the corresponding SQL statement on the changed elements.
Cascade: It always performs join operations on each element in the set.
(In the associated operation, Hibernate Determines whether the operation object has changed)

4.3.2 the two functions have different timing:
Cascade.
Inverse : When flush is enabled (commit will automatically execute flush, Hibernate Determine whether each set has changed,
If ( Inverse = True) return;

It can be seen that cascade comes first, Inverse After.

4.3.3 Inverse Set + One-to-sequence and set + sequence-to-sequence have different functions. Hibernate The generated SQL is different.
For one-to-least, Hibernate Execute the update Statement on the database table of the publisher.
For role-to-role, Hibernate Execute the insert/update/delte Statement on the relational table. Note that it is not a relational table but a relational table for the external database table.

Cascase is consistent with set, regardless of one-to-least or minus-to-least. The operation is passed to every element in the set. So it always updates the lifecycle
Database table.

4.3.4 suggestion: only set + sequence-to-Sequence Inverse = False. Other tags are not considered. Inverse Attribute, all set Inverse = True.

For cascade, cascade deletion is not set for one-to-one, including replicate-to-pair, and constrained = true.

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.