jpa implement CRUD

To simplify our own tools to write a JpaUtils
public class JpaUtils {
Private static Factory the EntityManagerFactory;
static {
Factory = Persistence.createEntityManagerFactory ( "myJpa");
}
public static getEntityManager the EntityManager () {
return factory.createEntityManager ();
}
}
explain: when the first call getEntityManager method will load the contents of a static block of code to create an entity manager inside the factory and then call the method to obtain the factory management class EntityManager this entity
carried out at a later call to get through direct seed plant
case I: inquiry
direct writing test class test:

test results:

Note that there is one caveat: that the previous configuration file has to be modified one:

To create modified to exist otherwise update the database table will be deleted'll get an error
there is another query:

Test results:

that in two ways what's the difference? ?
We marked break test: The first method testFind

click Next: Shortcuts f8

appeared sql statement (immediately loaded mode)

object is the entity class itself
test the second method testReference () marked with a breakpoint:


This method of obtaining proxy object is
find () method to query the current query object itself sql statement will be sent to query the database when calling find () method of
the object getReference () method to get is a dynamic proxy object calls getReference method does not immediately send sql statement when you call to query the database query result object will be sent when the sql statement when to use and when to send sql statement to query the database

Refresh operation test:

Test Results: console output

data database results:

Test deletion:

console output

after the database delete results:

Guess you like

Origin www.cnblogs.com/phantom576/p/11965969.html