Advanced object operations

Source: Internet
Author: User

1. Capture exceptions through getreference ()

 
Customer customer =Null;Try{Customer = em. getreference (customer.Class,NewINTEGER (1 ));}Catch(Entitynotfoundexception e ){// Custom capture exception}

 

2. Synchronize Databases

When the persist, merge, and remove methods are called, the state of the object is changed, but the object is not saved to the database.

The flush method is used to save objects to the database.

 
@ PersistencecontextProtectedEntitymanager em;PublicCustomer addcustomer (customer) {em. persist (customer); customer. setasset (NewDouble (1001.0 ));ReturnCustomer ;}

Assume that the value of asset is 1000.0. After the addcustomer method is called, the value of asset in the database is not 1000.0, but 1001.0.

When the method ends, entitymanager finds that the status of the customer instance object in the managed State has changed and automatically calls the flush method to save the latest object status to the database.

 

By default, flushmodetype is auto, and the Entity Manager Automatically calls the flush method at the right time to synchronously update the database. The user does not need to call the flush method at all;

Commit method: the flush method is called only after a transaction ends.

We recommend that you do not call the flush Method on your own, but hand over the Database Synchronization operation to the Entity Manager for automatic management, unless you need to forcibly Save the data.

 

3. Refresh and clear methods

Refresh reads data from the database into the entity

Clear converts all objects in the persistence context to the Free State. objects that have not been synchronized with the database are not persistently stored in the database.

The persistence context is the persistent environment in which objects are stored during Object Persistence interaction.

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.