Maven + EclipseLink实现的简单JPA例程

1. 使用maven创建一个jpa的测试工程

mvn archetype:create -DgroupId=myjpa -DartifactId=myjpa

2. 添加jpa所依赖的包

<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. 创建两个model类

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 @GeneratedValue(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><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 email; } public void setEmail(String email) { this.email = email; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public List<Address> getAddresses() { return addresses; } public void addAddress(Address address) { this.addresses.add(address); address.setCustomer(this); } @Override public String toString() { return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this); } }

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 @GeneratedValue(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; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getZipcode() { return zipcode; } public 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. 配置persistence.xml文件

在工程的src/main/resources目录下创建META-INF目录,并在META-INF下创建persistence.xml文件,内容如下:

<?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. 编写测试类

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.createEntityManagerFactory("eclipselink"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); for (int i = 0; i < 10; 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. 运行测试类,JPA会自动创建所要使用的数据库和表,然后就会执行相应的添加和查询操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值