JPA+Hibernate 3.3 ——第一个JPA程序
所需要的最小的jar包(注意:jar包所在路径不能含有空格或中文)
hibernate3.jar
hibernate-cglib-repack-2.1_3.jar
slf4j-api-1.5.2.jar
javassist-3.4.GA.jar
jta-1.1.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
log4j.jar
slf4j-log4j12.jar
hibernate-cglib-repack-2.1_3.jar
slf4j-api-1.5.2.jar
javassist-3.4.GA.jar
jta-1.1.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
log4j.jar
slf4j-log4j12.jar
创建实例类
package com.hujuan.bean; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity //默认情况下表名称是根据实体类名称创建的,name可以修改表名称 @Table(name="person_table") public class Person { private Integer id; private String userName; public Person(){ } public Person(String userName) { this.userName = userName; } //主键生成策略@GeneratedValue(strategy=GenerationType.AUTO), //值为AUTO表示根据数据库由Hibernate自动选择生成策略,也可以省略写@GeneratedValue //值为IDENTITY表示主键自增长 //值为SEQUENCE表示主键采用序列的方式 //值为TABLE各个数据库都通用,但效率较低 @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
注意:因为采用的是Hibernate所以必须有一个空的构造函数
JPA的配置文件
JPA规范要求配置文件在类路径的META-INF目录下放置名称为固定的的,即persistence.xml
<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_1_0.xsd" version="1.0"> <!-- 持久化单元,transaction-type事务类型包括全局事务类型JTA和本地事务类型 RESOURCE_LOCAL--> <persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL"> <properties> <!-- 数据库方言 --> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <!-- 建表方式,value值为creat-drop时表示创建应用的时候建表,结束应用的时候表自动删除; 值为update表示如果映射元数据不存在则建立表,如果映射元数据存在并新增加了字段则会添加到数据库表中 --> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="root"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpadb"/> </properties> </persistence-unit> </persistence>
测试类
package com.hujuan.test; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.BeforeClass; import org.junit.Test; import com.hujuan.bean.Person; public class PersonTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void save(){ //EntityManagerFactory相当于Hibernate中的sessionFactory //Persistence.createEntityManagerFactory("jpa")与配置文件中的持久化单元名称必须相同 EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa"); //EntityManager相当于Hibernate中session EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); //保存(持久化)方法 em.persist(new Person("Tom")); em.getTransaction().commit(); em.close(); factory.close(); } }