概述
openJPA是完全基于sun 公司的JPA规范而开发的,不像Hibernate,TopLink等其他JPA产品还有一套自己的应用规则。JPA是包含于EJB 3.0规范中的,JPA相当于EJB的persistence bean与dao。我在这里就不多废话了,只记录JPA相关的内容。
第一个应用
首先我们在eclipse中创建一个名为JPA的项目。并加入相应的包文件。这些包文件可以从http://openjpa.apache.org/ 获得。
1、在项目的src目录中需要创建一个META-INF目录,此目录是必需的(至少我是这么做的),并且在META-INF目录添加persistence.xml文件,内容如下:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> <persistence-unit name="mysql_jpa" transaction-type="RESOURCE_LOCAL"> <!-- ORM 类 --> <class>model.Person</class> <properties> <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" /> <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" /> <property name="openjpa.jdbc.DBDictionary" value="mysql"/> <property name="openjpa.ConnectionUserName" value="root"/> <property name="openjpa.ConnectionPassword" value="123456"/> <property name="openjpa.FetchBatchSize" value="20"/> <!-- 配置从java ORM annotation 生成数据库结构,配置此步时,需要把对应的java ORM类复制上面的<class/>标签中。 --> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true,PrimaryKeys=true,Indexes=true,schemaAction=refresh)"/> </properties> </persistence-unit> </persistence>
2、在model包先后添加如下文件:Gender.java、Person.java,它们的内容如下:
package model; public enum Gender { WOMAN, MAN }
package model; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity public class Person extends BaseObject { private static final long serialVersionUID = 2970135183705353062L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(length = 20, nullable = false) private String name; @Temporal(TemporalType.DATE) @Column(nullable = false) private Date birthday; @Enumerated(EnumType.STRING) @Column(nullable = false,columnDefinition=" enum('man','woman') NOT NULL DEFAULT 'man' ") private Gender gender = Gender.MAN; public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
3、在test包中添加PersonTest.java文件,内容如下:
package test; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import junit.framework.TestCase; import model.Person; public class PersonTest extends TestCase { public void testSave() { EntityManagerFactory factory = Persistence .createEntityManagerFactory("mysql_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Person p = new Person(); p.setName("林某某"); p.setBirthday(new Date()); em.persist(p); em.getTransaction().commit(); em.close(); factory.close(); } public void testFind() { EntityManagerFactory factory = Persistence .createEntityManagerFactory("mysql_jpa"); EntityManager em = factory.createEntityManager(); String jpql = "select count(p.id) from Person as p where 1=1 and p.name like ?1"; System.out.println(em.createQuery(jpql).setParameter(1, "%林%").getSingleResult()); em.close(); factory.close(); } }
4、右键PersonTest.java -> Run as -> Run configurations,在配置运行配置页中配置如下。
然后在以下VM arguments位置添加AspectJ的参数“-javaagent:${workspace_loc:JPA}/lib/openjpa-2.0.0-beta.jar”,如下图:
5、记录创建一个数据库,我是用test为名称。
6、运行Junit,输出消息如下:
78 mysql_jpa INFO [main] openjpa.Runtime - Starting OpenJPA 2.0.0-beta 156 mysql_jpa INFO [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.MySQLDictionary". 1140 mysql_jpa WARN [main] openjpa.jdbc.Schema - Existing column "gender" on table "person" is incompatible with the same column in the given schema definition. Existing column: Full Name: person.gender Type: char Size: 5 Default: man Not Null: true Given column: Full Name: Person.gender Type: varchar Size: 255 Default: null Not Null: true 16 mysql_jpa INFO [main] openjpa.Runtime - Starting OpenJPA 2.0.0-beta 16 mysql_jpa INFO [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.MySQLDictionary". 78 mysql_jpa WARN [main] openjpa.jdbc.Schema - Existing column "gender" on table "person" is incompatible with the same column in the given schema definition. Existing column: Full Name: person.gender Type: char Size: 5 Default: man Not Null: true Given column: Full Name: Person.gender Type: varchar Size: 255 Default: null Not Null: true 328 mysql_jpa INFO [main] openjpa.Runtime - Query "select count(p.id) from Person as p where 1=1 and p.name like ?1" is removed from cache excluded permanentlybecause this query returns a single value rather than a list. A query that returns single value can not be cached. . 5 |