Hibernate cascading Save and update

Source: Internet
Author: User
Tags manual writing

Cascading Save and update
When hibernate persists a temporary object, by default, he does not automatically persist other temporary objects associated with it, and if you want to persist all of the temporary objects that he is associated with when persisting an object: You can set the Cascade property to "Save-update". The default property value for Cascade is none.
Cascade: Cascading operations when manipulating objects, that is, cascading between tiers
Value Save-update: Indicates that when the current object (that is, insert and UPDATE statements) is saved and updated, it is cascaded to save and update the objects associated with him
Value all: The cascade operation is performed in any situation, that is, an object is manipulated and the other objects associated with it are manipulated
Value Delete: Indicates that the cascade operation is performed when the delete is executed, and the object associated with it is deleted
Value None: Indicates that no cascade operation is performed in any case
the inverse property of the <set> element

When you run the above program, if Hibernate's "Show-sql" is set to True, you will see that hibernate generates a lot of SQL statements, but many SQL statements are duplicated
eg
Insert into Test.order (o_name,c_id) VALUES (?,?)
Insert into Test.order (o_name,c_id) VALUES (?,?)
INSERT into Test order set c_id=? where id=?
INSERT into Test order set c_id=? where id=?

To solve this problem, we can use the <set> tag to add the inverse attribute. Terminology: Inverse refers to the meaning of reversal, in hibernate, in the direction associated relationship in the relationship, inverse= "false" the main control, the active party responsible for maintaining the object relationship we add in the object configuration file of the Customer object

<set name= "Orders" cascade= "Save-update" inverse= "true" >
<key column= "c_id" > </key>
<one-to-many class= "Net.mbs.mypack.Order"/>
</set>

Declared in the two-way relationship between customer and order, the customer-side association is only the image of the Order-side association (i.e., the order end is the primary null, responsible for maintaining the association between the customer and the Order object), When hibernate detects a change in the state of the customer or order of the persisted object (primarily the change of the association relationship), the database is updated only as the state of the order object changes.
By the time configuration, if in the program, we only use Customer.getorder (). Add (order) (involving the change of the relationship with the order), is not allowed to change the database and the object of the database synchronization update, The database updates the database according to changes only if the Order object state that was changed by using the method of the Order object (Eg:order.setCustomer (customer)----involves a change to the relationship with the customer). That is, only the state of the main control (involving changes to the associated relationship with the other side) will trigger a synchronized update of the object and the database.

Mapping a one-to-many two-way Association relationship
When an association between a class and a class is established, it is convenient to navigate from one object to another object or to a set of objects associated with him, and according to the above program, if you want to get the customer object associated with him for a given order object, we only need to invoke the following method:
Customer C=order.gecustomer (); So what should we do when we get a customer object and want to find out all the order objects associated with this customer object? Because an association with an order object is not established in the customer, it is not possible to automatically load all the order objects associated with him by loading the customer object. The only way to query the database order table to return the required information is through the JDBC or SQL language manual writing code.
The above problem is solved, but not the best, because this performance will drop, the object is in memory, navigating from one object to another in memory is obviously much faster than querying the data in the database.

Concrete implementation <br>
1: Because the customer and order is a one-to-many, that is, a customer to correspond to more than a few, so a set object should be established in the customer to store all the order objects associated with this customer object.
2: In Customer.hbm.xml through <one-to-many> to establish the relationship between the order table
Note that:<one-to-many> should be placed in the <set> tag
Let's take a look at the design of the customer class and the contents of the Customer.hbm.xml file
<br><br><br>------------------------------------------------------
Customer Order bi-directional one-to-many
Create a container object in the 1:customer class that contains all the associated order objects
Create a Customer object in the 2:order class that associates the customer
Inverse= "True" indicates that the rights associated with the maintenance are given to generate the Hibernate statement

Customer.getorders (). Add (order);
Customer.setname ("dddddd");

Inverse= "true" (the party that sets this property----is the accused)
let hibernate generate SQL statement   when the host modifies the association relationship between objects;

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.