Specific examples
After completion of the project structure such as:
(Note: The location of the Persistence.xml file determines the root of persistence (persistence root). The root of the persistence is the jar file or the directory containing the Meta-inf directory (provided that Persistence.xml is located here). This persistence.xml file is generally placed in the Meta-inf under SRC. The name and position are not changed)
A), import the relevant jar package (see) and create the Meta-inf and Persistence.xml files.
The Persistence.xml is configured as follows: (my data is MySQL, different implementations of different databases and JPA will result in different configuration content)
[XHTML]View PlainCopy
- <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemalocation="Http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ Persistence_version=1 ">
- <persistence-unit name="MYSQLJPA" transaction-type="resource_local">
- <properties>
- <property name= "hibernate.dialect" value="Org.hibernate.dialect.MySQL5Dialect" / >
- <property name= "hibernate.connection.driver_class" value="Com.mysql.jdbc.Driver" / >
- <property name= "hibernate.connection.username" value="root" />
- <property name= "hibernate.connection.password" value="123456" />
- <property name= "hibernate.connection.url" value="JDBC:MYSQL://LOCALHOST:3306/DB1" />
- <property name= "hibernate.max_fetch_depth" value="3" />
- <property name= "hibernate.hbm2ddl.auto" value="Update" />
- </Properties>
- </persistence-unit>
- </Persistence>
b), write the entity bean, as follows:
[C-sharp]View PlainCopy
- Package Com.hmk.bean;
- Import Javax.persistence.Column;
- Import javax.persistence.Entity;
- Import Javax.persistence.GeneratedValue;
- Import Javax.persistence.Id;
- @Entity
- Public class Person {
- private int id;
- private String name;
- @Id @GeneratedValue
- public int getId () {
- return ID;
- }
- public void setId (int id) {
- this.id = ID;
- }
- @Column (length=12)
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }
c), write the JUnit test code as follows:
[Java]View PlainCopy
- Package junit.test;
- Import Javax.persistence.EntityManager;
- Import Javax.persistence.EntityManagerFactory;
- Import javax.persistence.Persistence;
- Import Org.junit.BeforeClass;
- Import Org.junit.Test;
- Import Com.hmk.bean.Person;
- Public class Jpatest {
- @BeforeClass
- public static void Setupbeforeclass () throws Exception {
- }
- @Test public void CreateTable () {
- //Can verify that the build table is correct
- Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
- Factory.close ();
- }
- @Test public Void Save () {
- Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
- Entitymanager em = Factory.createentitymanager ();
- Em.gettransaction (). Begin ();
- Person person = new Person (); //person to new state
- Person.setname ("Zhang San");
- Em.persist (person); //Persistent entities
- Em.gettransaction (). commit ();
- Em.close ();
- Factory.close ();
- }
- //new, escrow, removal, removal
- @Test public Void Update () {
- Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
- Entitymanager em = Factory.createentitymanager ();
- Em.gettransaction (). Begin ();
- Person person = em.find (person. class, 1);
- Person.setname ("HMK"); //person is a managed state
- Em.gettransaction (). commit ();
- Em.close ();
- Factory.close ();
- }
- @Test public void Update2 () {
- Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
- Entitymanager em = Factory.createentitymanager ();
- Em.gettransaction (). Begin ();
- Person person = em.find (person. class, 1);
- Em.clear (); //Change all entities in the entity manager to the off-pipe State
- Person.setname ("Hmk2");
- Em.merge (person); //Turn off state into managed state, merge can automatically select INSERT or update data
- Em.gettransaction (). commit ();
- Em.close ();
- Factory.close ();
- }
- @Test public void Remove () {
- Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
- Entitymanager em = Factory.createentitymanager ();
- Em.gettransaction (). Begin ();
- Person person = em.find (person. class, 1);
- Em.remove (person); //Delete entity
- Em.gettransaction (). commit ();
- Em.close ();
- Factory.close ();
- }
- @Test public Void Find () {
- Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
- Entitymanager em = Factory.createentitymanager ();
- Person person = em.find (person. Class, 2); //A Get method similar to hibernate, returns NULL when no data is found
- System.out.println (Person.getname ());
- Em.close ();
- Factory.close ();
- }
- @Test public void Find2 () {
- Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
- Entitymanager em = Factory.createentitymanager ();
- Person person = em.getreference (person. Class, 2); //Similar to Hibernate's Load method, lazy loading. An exception occurs when there is no corresponding data
- System.out.println (Person.getname ()); //Real call to find data
- Em.close ();
- Factory.close ();
- }
- }
D), run the appropriate method in the JUnit test code.
JPA Instances-Reprint