1、persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="jpa" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.username" value=" " />
<property name="hibernate.connection.password" value=" " />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxxx:1521:xxx" />
<property name="hibernate.max_fetch_depth" value="5" />
<property name="hibernate.jdbc.fetch_size" value="20" />
<property name="hibernate.jdbc.batch_size" value="10" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
2 实体类
package entity;
//认准是这个包
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
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 javax.persistence.Transient;
@Entity
@Table(name="t_person")
public class Person {
private Integer id;
private String name;
private Date birthday;//1988-12-24
private Gender gender=Gender.MAN;//设置默认值
private String info;
private Byte[] file;//二进制文本
private String imagePath;
//此无参构造器必须要有
public Person() {}
public Person(String name) {
this.name = name;
}
//(strategy=GenerationType.IDENTITY)数据库id自增长的方式生成主键
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length=10,nullable=false,name="personName")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(TemporalType.DATE) @Column(nullable=false)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
//@Enumerated(EnumType.ORDINAL) 此种注解在数据库中保存索引值,即0,1,2..
//此种注解在数据库中保存字符串,易于理解,可读性高; 保存枚举类型时一定要设置非空约束
@Enumerated(EnumType.STRING) @Column(length=5,nullable=false)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Lob //大文本字段注解
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
//@Basic(fetch=FetchType.EAGER) 代表立刻加载
@Lob @Basic(fetch=FetchType.EAGER) //代表延时加载,也就是当我们不需要访问此属性时,读取时不会从数据库中抓取此属性值
public Byte[] getFile() {
return file;
}
public void setFile(Byte[] file) {
this.file = file;
}
@Transient //不作为持久化字段,也就是说不和数据库建立映射关系
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
}
枚举类
package entity;
public enum Gender {
MAN,//索引值为0
WOMEN//索引值为1
}
3、 测试类的测试方法
@Test
public void mytest(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa");
factory.close();
}
4 、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxxxx.mvc</groupId>
<artifactId>jpa</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>jpa Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.8.Final</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
<type>jar</type>
</dependency>
<!-- mysql驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
-->
<!-- oracle驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10</version>
</dependency>
</dependencies>
<build>
<finalName>jpa</finalName>
</build>
</project>