1. Use Maven to create a JPA test project
MVN ARCHETYPE:CREATE-DGROUPID=MYJPA-DARTIFACTID=MYJPA
2. Add the packages that JPA relies on
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> < configuration> <source>1.5</source> <target>1.5</target> </configuration> </ plugin> </plugins> </build> <repositories> <repository> <id>eclipselink repo</id > <url>http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo </url> </repository> </repositories> <dependencies> <dependency> <groupId> Commons-lang</groupid> <artifactId>commons-lang</artifactId> <version>2.4</version > </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactid >javax.persistence</artifactId> <version>2.0.0</version> </dependency> <dependency > <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>1.8.0.7</version> </dependency> < dependencies>
3. Create two model classes
Customer.java
Package Myjpa.model; Import java.io.Serializable; Import java.util.ArrayList; Import Java.util.Date; Import java.util.List; Import javax.persistence.Entity; Import Javax.persistence.GeneratedValue; Import Javax.persistence.GenerationType; Import Javax.persistence.Id; Import Javax.persistence.OneToMany; Import javax.persistence.Temporal; Import Javax.persistence.TemporalType; @Entity public class Customer implements Serializable {private static final long serialversionuid = 1L; @Id @GeneratedVal UE (strategy = generationtype.table) private int id; private String name; Private String Email; @Temporal (temporaltype.date) private DATE birthday; @OneToMany (Mappedby = "Customer") private final list<address> addresses = new arraylist<address> (); public int getId () {<mce:script type= "Text/javascript src=" http://hi.images.csdn.net/js/blog/tiny_mce/themes/ Advanced/langs/zh.js "mce_src=" Http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js "></ Mce:script>&lT;mce:script type= "Text/javascript" src= "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js "Mce_src=" Http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js "></mce:script> return ID; public void setId (int id) {this.id = ID;} public String GetName () {return name;} public void SetName (String name) { THIS.name = name; public string Getemail () {return e-mail;} public void Setemail (String email) {this.email = email:} Public Date Getbir Thday () {return birthday.} public void Setbirthday (Date birthday) {this.birthday = birthday;} public LIST<ADDRESS&G T GetAddresses () {return addresses.} public void AddAddress (address) {this.addresses.add; Address.setcu Stomer (this); @Override public String toString () {return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString ); }}
Address.java
Package Myjpa.model; Import java.io.Serializable; Import javax.persistence.Entity; Import Javax.persistence.GeneratedValue; Import Javax.persistence.GenerationType; Import Javax.persistence.Id; Import Javax.persistence.ManyToOne; @Entity public class address implements Serializable {private static final long serialversionuid = 1L; @Id @GeneratedValu E (strategy = generationtype.table) private int id; private String name; Private String description; Private String ZipCode; @ManyToOne private customer customer; public int getId () {return id;} public void setId (int id) {this.id = ID;} public String GetName () {return name; pub LIC void SetName (string name) {this.name = name;} public String getdescription () {return description;} public void Set Description (String Description) {this.description = Description;} public String Getzipcode () {return zipcode; void Setzipcode (String zipcode) {this.zipcode = zipcode} @ManyToOne public Customer GetCustomer () {return customer;public void Setcustomer (customer customer) {This.customer = customer;} @Override public String toString () {return org . apache.commons.lang.builder.ToStringBuilder.reflectionToString (this); }}
4. Configure Persistence.xml Files
Create the Meta-inf directory under the Src/main/resources directory of the project and create the Persistence.xml file under Meta-inf, which reads as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <persistence version= "1.0" 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_1_0.xsd "> <persistence-unit name=" Eclipselink "> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class> myjpa.model.customer</class> <class>myjpa.model.Address</class> <properties> <property Name= "Javax.persistence.jdbc.driver" value= "Org.hsqldb.jdbcDriver"/> <property name= " Javax.persistence.jdbc.url "Value=" Jdbc:hsqldb:mem:. "/> <property name=" Javax.persistence.jdbc.user "value=" Sa "/> <property name=" Javax.persistence.jdbc.password "value=" "/> <property name=" Eclipselink.ddl-generation "value=" Drop-and-create-tables "/> <property name=" Eclipselink.ddl-generation.output-mode "value=" Database "/> </properties> </persistence-unit> </persistence>
5. Writing Test class
Package MYJPA; Import Java.util.Calendar; Import java.util.List; Import Javax.persistence.EntityManager; Import Javax.persistence.EntityManagerFactory; Import javax.persistence.Persistence; Import Javax.persistence.TypedQuery; Import myjpa.model.Address; Import Myjpa.model.Customer; public class Test {public static void main (string[] args) throws Exception {Entitymanagerfactory factory = persistence.c Reateentitymanagerfactory ("Eclipselink"); Entitymanager em = Factory.createentitymanager (); Em.gettransaction (). Begin (); for (int i = 0; i < i++) {Customer customer = new Customer (); Customer.setname ("customer" + i); Customer.setemail ( "Customer" + i + "@my. com"); Customer.setbirthday (Calendar.getinstance (). GetTime ()); Address addresshome = new address (); Addresshome.setname ("Home"); Addresshome.setdescription ("Home"); Addresshome.setzipcode ("123456"); Em.persist (Addresshome); Address Addressoffice = new address (); Addressoffice.setname ("Office"); Addressoffice.setdescription ("OffIce "); Addressoffice.setzipcode ("654321"); Em.persist (Addressoffice); Customer.addaddress (Addresshome); Customer.addaddress (Addressoffice); Em.persist (customer); Em.gettransaction (). commit (); Em.close (); EM = Factory.createentitymanager (); typedquery<customer> q = em.createquery ("Select C from Customer C", Customer.class); List<customer> customers = Q.getresultlist (); for (Customer c:customers) {System.out.println (c);} em.close (); }}
6. Run the test class, JPA will automatically create the database and tables to use, and then perform the appropriate add and query operations.