Objective: To understand the use of the Entity Manager and Entity Manager factory. Main Content: U javax. persistence. persistence Class U javax. persistence. entitymanagerfactory interface U javax. persistence. entitymanager interface U entitymanagerfactory object acquisition U entitymanager object acquisition U usage 1. javax. persistence. the persistence class is used to obtain entitymanagerfactory. There are two main methods: U createentitymanagerfactory (string persistenceunitname), create and return the entitymanagerfactory corresponding to the persistent unit name specified by the parameter. U createentitymanagerfactory (string persistenceunitname, map properties), create and return entitymanagerfactory. The first parameter is the name of the persistent unit, and the second parameter is the attribute required to create entitymanagerfactory, these attributes overwrite the attributes set elsewhere. Both methods are static methods and can be accessed directly by class name. 2. Entity Manager factory entitymanagerfactory Entity Manager factory is used to manage entity managers and is an object of entitymanagerfactroy. The main method is as follows: U createentitymanager (), create the Entity Manager U createentitymanager (MAP map), create the Entity Manager U close () based on the specified attributes, and close the Entity Manager factory, release the resource U isopen () and determine whether the physical manager factory has closed the interface. For details, see section 356. 3. operations performed by Entity Manager on objects are completed by Entity Manager. The main method is as follows (page 357 of the interface definition reference): U persist (Object O), Persistent Object U Merge (t o), merged object state u remove (Object entity ), delete the entity u find (class entityclass, object primarykey), find the entity U flush () based on the primary key, synchronize it to the database U setflushmode (flushmodetype flushmode), and set the synchronization mode U getflushmode (), obtain the synchronization mode U Lock (Object entity, lockmodetype lockmode), lock U refresh (Object entity), update the state u clear (), clear the persistent context U contains (Object entity ), determine whether the object belongs to the current context U createquery (string sqlstring) and create a query U createnamedquery (string name), create a name query U createnativequery (string sqlstring), create a standard SQL query U createnativequery (string sqlstring, class resultclass), and create a standard SQL query, specify the query result U createnativequery (string sqlstring, string resultsetmapping), create a standard SQL query, and specify how the query result maps to u gettransaction (), returns the transaction object. 4. The Entity Manager factory obtains the Entity Management factory which can be injected or obtained through the persistence class. 4.1 getting Entity Manager through injection the Entity Manager factory can use meta annotation injection. You can use the following meta annotation: javax. Persistence. persistenceunit to indicate a dependency on entitymanagerfactory. The attributes are as follows: U attribute name: the name used to access entitymanagerfactory in the context of environment reference (in the Environment referencing context,). If dependency injection is used, it is not required. The default value is a null string. U attribute unitname: name of the persistent unit defined in persistence. xml. If you want to specify, the persistent unit of the Entity Manager factory used in JNDI should be the same as the name. For example: @ persistenceunitentitymanagerfactory EMF; 4.2 get entitymanagerfactory EMF = persistence through the persistence class. createentitymanagerfactory ("orderem"); 5. You can use the following methods to obtain the Entity Manager: U injection to the Entity Manager; and U search for the Entity Manager; U uses the Entity Manager factory to create the Entity Manager. The following sections describe each other. 5.1 The injection object manager can inject the object manager into the application. The injection object manager can use the following meta Note: javax. Persistence. persistencecontext to indicate a dependency on the persistent context of the entitymanager. The attributes in the meta-annotation are as follows: U attribute name: the name used to access the object manager in the context of environment reference (in the Environment referencing context,). If dependency injection is used, no. The default value is a null string. U attribute unitname: name of the persistent unit. If the unitname element is specified, it must be the same name as the persistent unit of the Object Manager that can be accessed in JNDI. The default value is a null string. The U attribute type specifies the transaction type: the transaction type includes extedned (Extended persistent context) and transaction (and the persistent context of the transaction scope ). N persistencecontexttype. extendedn or persistencecontexttype. transaction. U property properties: Specify properties for the container or persistent provider. Attributes related to specific persistence providers can also be included in these attributes. Properties that the provider does not recognize will be ignored. The default value is {}. For example: @ persistencecontextentitymanager em; If there are multiple persistent units in the current application, you need to specify the name of the persistent unit. 5.2 find the object manager by simply searching for the Entity Manager: declare: @ persistencecontext (name = "orderem") inject sessioncontext: @ resource sessioncontext CTX; Search: entitymanager em = (entitymanager) CTX. lookup ("orderem"); 5.3 create an Entity Manager through the Entity Manager factory is the object of entitymanagerfactory. Assuming that the object of the Entity Manager factory is EMF, the code for creating the Entity Manager is as follows: entitymanager em = EMF. createentitymanager (); 6. Use javax. persistence. persistence creates the entitymanagerfactory object, and then creates the en through the entitymanagerfactory object Titymanager object, and then entitymanager object. In the Java EE environment, in addition to the above method, the entitymanager object and entitymanagerfactory object can be injected. If entitymanagerfactory is injected, the entitymanager object is created. Tutorial: Java ee 5 practical tutorial-Based on WebLogic and eclipse