Click to access: JPA environment configuration (i)
Persistence:
Persistence is used to get the Entitymanagerfactory instance, which contains a static method named Createentitymanagerfactory .
There are two overloaded versions of this method:
1) A method with a parameter is named as a parameter in the JPA configuration file for the persisted unit.
<name= "JpaDemo1" transaction-type= "resource_local">
That's the JpaDemo1.
2) A method with two parameters: the first parameter is the same as the first version, and the second parameter passes through a map collection, which can be used to set the JPA configuration.
Entitymanagerfactory:
This interface is primarily used to create an Entitymanager instance object, which is constrained by 4 methods.
Createentitymanager (): instance used to create an entity manager object
Createentitymanager (Map map): The map parameter is used to provide Entitymanager properties
IsOpen (): Used to detect if the Entitymanagerfactory object is closed, unless call Close () is closed after the factory is opened
Close (): Above also said, shut down the factory used, after closing in use throw exception illegalstateexception
Entitymanager:
In the JPA specification, Entitymanager is the core object of the persistence operation, and the entity acts as a normal Java object, only to be persisted after the call to Entitymanager to cross it with the database. The Entitymanager object manages the O/R mapping between a set of entity classes and the underlying data source. It can be used to manage and update the entity Bean, find the entity bean based on the primary key, and query the instance through the JPQL statement.
The state of the entity is divided into:
1) New state: Newly created object with no persisted primary key
2) Persistent state: already has a persistent primary key and persistence establishes the context
3) Free State: Has a persistent primary key, but does not establish a contextual environment with persistence
4) Delete state: Has persisted primary key, has established the context with persistence, but the database has been deleted.
Now test how the methods in this class are used.
Find (class<t> entityclass, Object PrimaryKey)
The first parameter, Entityclass, is the class object of the persisted class, the second parameter is the primary key ID, the same hibernate seed get () method, and once the execution is immediately loaded, the statement is sent instantly
// gets the persisted object, sends the SQL statement immediately, does not find the corresponding data return null Student Student = Entitymanager.find (Student. Class, 1); SYSTEM.OUT.PRINTLN (student);
GetReference (class<t> entityclass, Object PrimaryKey)
The first parameter, Entityclass, is the class object of the persisted class, and the second parameter is the primary key ID. With hibernate in the load () method, the Send statement is used even if execution is used to load
@Test Public void test3 () { // gets persisted object, deferred load uses only send SQL statement, no query to corresponding data throws exception Student Student = Entitymanager.getreference (Student. Class, 1); SYSTEM.OUT.PRINTLN (student); }
Persist (Object entity)
Used to incorporate new objects into the management of Entitiymanager, which becomes a persisted object after the method executes. If you are managing an object that is already persisted then this method will not do anything, and passing in the deleted state object will persist the object. An exception can be thrown if an object in the Free State is passed in.
// To convert a new object to a persisted object New Student ("AA", "AA-00", 11, "13212341234", "China-unknown-unknown"); Entitymanager.persist (student);
Remove (Object entity)
Delete the instance, only the persisted object is deleted with Hibernate.
@Test publicvoid test5 () { // Delete a persisted object, Database corresponding records will be deleted Student Student = Entitymanager.find (Student. Class, 1); Entitymanager.remove (student); }
CreateQuery (String hqstring)
Create a Query object
@Test Public void CreateQuery () { // Create a Query object String hql = "from Student"; = Entitymanager.createquery (HQL); List<Student> students = query.getresultlist (); for (Student student:students) { System.out.println (Student); } }
Createnamedquery (String namestring)
Creates a query object based on a named query statement block. The parameter is a named query statement.
Createnativequery (String sqlString):
Create a Query object using standard SQL statements. parameter is a standard SQL statement string.
@Test Public voidCreatenativequery () {//Create a Query object using standard SQL statements. parameter is a standard SQL statement string. String sql = "SELECT * FROM Tal_student"; Query Query= Entitymanager.createnativequery (sql,student.class); List<Student> students =query.getresultlist (); for(Student student:students) {System.out.println (Student); } //createnativequery (String sqls, string resultsetmapping)//creates a query object using standard SQL statements and specifies the name of the map that returns the result set. }
Merge (T entity)
Same as Hibernate saveorupdate method, used for persistent object synchronization If there is no insert if there is an update performed
Flush ()
Synchronous persistent context means that all unsaved data in the persistence context is updated to the database
Refresh ()
Data for updating an entity record
Contains (Object entity)
Determines whether an object is a persisted object
IsOpen ()
Determine if the Entity manager is open
Gettransaction ()
Returns the resource-level transaction object.
Close ()
Close this entity manager
entitytransaction
An interface is used to manage transaction operations for the resource-tier entity manager. The instance is obtained by invoking the Gettransaction method of the entity manager.
There are several common methods in this and hibernate are the same transaction.
begin () start a transaction if this thing has started throwing an exception
Commit () commits a thing and updates the add-in operation to the database after submission
rollback () rolls back one data, and all operations are invalidated after the rollback.
Note that my above operation code is the unit test, @Before has been the initial chemical plant and the entity manager, in the @after to commit the matter and the resource closed, not committed transactions, above those additions and deletions are invalid operations.
JPA core classes and use (ii)