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.