Entitymanager is an auxiliary class that is used to manipulate entity beans. He can be used to generate/delete persisted entity beans, find entity beans by primary key, or find entity beans that satisfy a condition through the EJB3 QL language. When an entity bean is managed by Entitymanager, Entitymanager tracks his state changes and synchronizes the changed values to the database when any decision is made to update the entity bean. When the entity bean is detached from Entitymanager, he is not managed and Entitymanager cannot track any of his state changes. The acquisition of Entitymanager has been described previously and can be injected dynamically by the EJB container through @persistencecontext annotations, for example:
@PersistenceContext (unitname= "Foshanshop")
entitymanager em;
Entitymanager Common methods:
1.Entity of Access
Find (*.CLASS,ID): Returns null if the entity bean does not exist
GetReference (*,class,id): throws javax.persistence.EntityNotFoundException if the entity bean does not exist, and does not guarantee that the entity bean has been initialized
Note: If *.class is not an entity bean, it will cause illegalargumentexception
2.persist (): Add entity Bean
3. Update the entity Bean: When the entity is being managed by the container, you can invoke the entity's set method to modify the data, and when the container determines flush, the updated data is synchronized to the database. If you want the modified data to be synced to the database in real time, you can execute the Entitymanager.flush () method.
4.merge (): is used when the entity bean has been managed out of Entitymanager, when the container decides to flush, the data will be synchronized to the database, and when the Em.merge (Object obj) method is executed, the container's working rules:
(1). If a container-managed object instance with the same ID already exists in the container, the container will copy the contents of the parameter obj into the managed instance, and the merge () method returns the managed instance, but the parameter obj is still detached and is not managed. The container synchronizes the instance to the database when it decides to flush.
(2). An object instance with the same ID does not exist in the container. The container will copy a container-managed object instance from the passed obj parameter, and the merge () method returns the managed instance, but the parameter obj is still detached and is not managed. The container synchronizes the instance to the database when it decides to flush. If the parameter passed into the merge () method is not an entity bean, it throws a illegalargumentexception
5.Remove (): Delete Object
6.createQuery () Returns the query object to execute the JPQL statement
7.createNativeQuery () Returns the query object to execute the SQL statement
8.refresh () Refreshes the entity bean to get the new object (elegant to get the newest object method)
9.contains () detects whether the entity is currently managed
The method uses an entity as a parameter, and if the entity object is currently managed by persisted content, the return value is true, otherwise false
10.clear () Detach all entities that are currently being managed
When dealing with a large number of entities, you will consume a lot of memory if you do not separate the processed entities from the Entitymanager. After calling Entitymanager's clear () method, all the entities being managed will be detached from the persisted content.
It is important to note that before a transaction is committed (the transaction defaults to the last commit of the call stack, such as the return of the method), if the clear () method is called, any changes previously made to the entity will be lost, so it is recommended that you call the flush () method to save the changes before calling the clear () method. Flush () Refreshes the entity's changes immediately into the database
When an entity Manager object is used in a session bean, it is bound to the transaction context of the server. The Entity manager commits and synchronizes its contents when the server's transaction commits. In a session bean, the server's transaction defaults to the last commit of the call stack (for example, the return of the method)
12.javax.persistence.flushmodetype flush mode for Entity Bean Manager
Flushmodetype.commit : Refresh occurs only when a transaction commits, where a query statement (except for the
find () and getreference () queries) exists during a large amount of data updates Flushmodetype.auto:(default mode) refresh occurs before the query statement executes (except for the Find () and getreference () queries) or transaction commits : There are no query statements in the process of updating the data in large numbers (except for the Find () and getreference () query) execution.
The number of times the JDBC driver interacts with the database. The greatest improvement in JDBC performance is to reduce the network traffic between the JDBC driver and the database Flushmodetype.commit mode enables updates to be done only once in a network interaction, while the Flushmodetype.auto mode may require multiple interactions (how many times the flush How many network interactions are generated)
Setting: Entitymanager.setflushmode (Flushmodetype.commit);
13.getDelegate () Gets a reference to the persisted implementation
Using the Getdelegate () method, you can obtain references to Entitymanager persistence implementations, such as the persistent product of JBoss EJB3 with Hibernate, which can be accessed through the Getdelegate () method, such as:
@PersistenceContext
protected Entitymanager em;
Hibernateentitymanager manager = (Hibernateentitymanager) em.getdelegate ();
After you have obtained a reference to hibernate, you can encode it directly against hibernate, but this method is not advisable and is highly recommended. In WebLogic, you can also get access to Kodo by using this method
Another: processing when a mapped table or column name has the same name as a database reserved word
Use the table name spike, for example: under MySQL, with ' order ', or under SQL Server [TableName], but this is not suitable for program porting
Turn from:
http://blog.163.com/zhouhuoxiang12@126/blog/static/88776461200945112658420/