Hibernate Learning (5): Session. Update

Source: Internet
Author: User

Note the following points for the update method of the session interface:

1. input parameters

Generally, objects passed to update are in the Free State. If a persistent object is uploaded, the update method is redundant, because the hibernate dirty check mechanism will automatically send an update statement to the database based on the changes in the object property value; if the input object is in the temporary state, Hibernate will throw an exception. Hibernate searches for and updates corresponding records based on the oId of the object during data update, but no records in the database are associated with this temporary object, therefore, Hibernate will throw an exception. Of course, if you manually specify an oId for the temporary object, you should consider it another way, as shown in the following code snippet:

................................................

Customer customer = new customer ();

Customer. setid (3l );

Customer. setname ("cindyelf ");

Session. Update (customer );

................................................

This code will result in the following SQL statement: Update customer set name = 'cindyelf' where id = 3; of course, if the database does not have the row with ID 3, Hibernate will throw an exception. The specified OID for a temporary object is an nonstandard operation and should be avoided as much as possible. That is to say, no matter what state object is passed in, the database must have a record corresponding to the oId of this object, otherwise an exception is thrown.

2. Operation

When the update method is executed, Hibernate first puts the passed object into the session cache for persistence, and then plans to execute an update statement. When hibernate generates an SQL statement, it assembles the SQL statement based on the current attribute value of the object. That is, no matter how many times the attribute value is modified in the program, only one update statement is executed during execution.

In addition, the update API specifically emphasizes that "if a persistence object in the session cache has the same OID as the object to be updated, Hibernate will throw an exception ". The following code snippet demonstrates this error:

................................................

Customer customer = new customer ();

Session1.save (customer );

................................................

................................................

Customer customer1 = (customer) Session. Load (customer. Class, new long (6 ))

Session2.update (customer );

................................................

As shown above, I have persisted a customer object in session1, and its oid is 6. Then, in session2, I load a customer1 object whose oid is 6, then, the customer Before update in session2. Note that for session2, customer1 is in the Free State because it is not in the cache of session2. The program reports the following error: a different object with the same Identifier value was already associated with the session.

 

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.