JPA(hibernate)开发环境的搭建和一个基本的实例
实现产品是Hibernate
需要的jar:
hibernate-entitymanager-4.1.7.Final.jar
dist\lib\provided\jta-1.1.jar
required\*
JAR包列表如下:
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.7.Final.jar
hibernate-entitymanager-4.1.7.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
jta-1.1.0.jar
配置文件必须放在根目录(src)的META-INF\persistence.xml
<?xml version="1.0"?>
<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">
<persistence-unit name="myPersistence" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<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/db_jpa?useUnicode=true&
characterEncoding=UTF-8" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
枚举类型:
public enum Gender {
MAN,WOMAN
}
实体Bean:
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.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import com.yjdgis.enumation.Gender;
@Entity
@Table(name="tbl_person")
public class Person {
private Integer id;
private String name;
private Date birthday;
private Gender gender = Gender.MAN;
private String context;
private String imagePath;
public Person(){}
public Person(String name) {
this.name = name;
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)//默认是auto
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="person_name",length=10,nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Enumerated(EnumType.STRING)
@Column(length=5,nullable=false)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Lob
@Basic(fetch=FetchType.LAZY)//大字段延迟加载
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
@Transient//这个注解用来标注imagePath这个属性不作为可持久化字段,就是说不跟数据库的字段做任何关联
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
}
测试类:
package junit.test;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.Test;
import com.yjdgis.entity.Person;
public class PersonTest {
@Test
public void test() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("myPersistence");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person p = new Person("黄文静");
p.setBirthday(new Date());
em.persist(p);
em.getTransaction().commit();
em.close();
factory.close();
}
}