persistence.xml 需放在类路径下META-INFO文件夹下面
<?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">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties><!-- 使用提供者属性 -->
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.url"
value="jdbc:oracle:thin:@192.168.17.96:1521:tx431" />
<property name="hibernate.connection.driver_class"
value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.username" value="x431" />
<property name="hibernate.connection.password" value="x431" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Person2.java
package com.launch.jpa;
import java.io.Serializable;
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.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@Table(name = "person")
public class Person2 implements Serializable {
private int id;
private String name;
private java.util.Date birthday;
@Enumerated(EnumType.ORDINAL)
// @Enumerated(EnumType.STRING)
@Column(length = 5, name = "gender")
// 设置默认值
private String gender ;
//大数据类型,延迟加载 (立即加载EAGER)
private String info;
//不和数据库做映射
private String image;
@Lob @Column(name="infor")@Basic(fetch=FetchType.LAZY)
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
//日期映射
@Temporal(TemporalType.TIMESTAMP)
public java.util.Date getBirthday() {
return birthday;
}
public void setBirthday(java.util.Date birthday) {
this.birthday = birthday;
}
@Column(name = "name", length = 20, nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PDT_TYPE_seq")
@SequenceGenerator(name = "PDT_TYPE_seq", sequenceName = "PDT_TYPE_SEQ")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Transient
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
测试
TestJpa.java
package com.launch.jpa;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* <b>Title:</b> TestJpa.java<br>
* <b>Description:</b>实体的状态
实体对象拥有以下4个状态,这些状态通过调用EntityManager接口方法发生迁移:
1) 新建态:新创建的实体对象,尚未拥有持久化主键,没有和一个持久化上下文关联起来。
2) 受控态:已经拥有持久化主键并和持久化上下文建立了联系;
3) 游离态:拥有持久化主键,但尚未和持久化上下文建立联系;
4) 删除态:拥有持久化主键,已经和持久化上下文建立联系,但已经被安排从数据库中删除。
<br>
* Copyright (c) 2010-2011 Launch Tech Co., Ltd. All rights reserved.
* @author zhangkui Nov 17, 2010
* @version V1.0
*/
public class TestJpa {
static EntityManagerFactory factory;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
factory = Persistence.createEntityManagerFactory("jpa");
}
// 新建状态 ,托管状态,游离状态,删除状态
@Test
public void save() {
try {
EntityManager eManager = factory.createEntityManager();
eManager.getTransaction().begin();
Person type = new Person();
type.setName("test");
//type.setFile(("long long long time ago").getBytes());
type.setInfo("long long long time ago");
type.setBirthday(new Date());
eManager.persist(type);
eManager.getTransaction().commit();
eManager.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getPerson() {
try {
// 内部封装了hibernate session对象
EntityManager eManager = factory.createEntityManager();
Person2 type = eManager.find(Person2.class, 5007050);
// equals hibernate get
System.out.println(type.getName());
eManager.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getPerson2() {
try {
EntityManager eManager = factory.createEntityManager();
Person2 type = eManager.getReference(Person2.class, new Integer(
5006250));
// equals hibernate load
System.out.println(type.getId());
eManager.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void update() {
try {
EntityManager eManager = factory.createEntityManager();
eManager.getTransaction().begin();
Person2 type = eManager.find(Person2.class, new Integer(5006850));
type.setName("test");
eManager.clear();
type.setName("test2");
eManager.merge(type);
eManager.getTransaction().commit();
eManager.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void delete() {
try {
EntityManager eManager = factory.createEntityManager();
eManager.getTransaction().begin();
Person2 type = eManager.find(Person2.class, new Integer(5006850));
eManager.remove(type);
eManager.getTransaction().commit();
eManager.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void query() {
try {
// 内部封装了hibernate session对象
EntityManager eManager = factory.createEntityManager();
Query query = eManager.createQuery("select p from Person2 p where p.id=?3 ");
query.setParameter(3, 5007050);
List list = query.getResultList();
System.out.println(list.size());
eManager.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void deleteQuery() {
try {
// 内部封装了hibernate session对象
EntityManager eManager = factory.createEntityManager();
eManager.getTransaction().begin();
Query query = eManager.createQuery("delete from Person2 p where p.id=?3 ");
query.setParameter(3, 5007050);
query.executeUpdate();
eManager.getTransaction().commit();
eManager.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void queryUpdate() {
try {
// 内部封装了hibernate session对象
EntityManager eManager = factory.createEntityManager();
eManager.getTransaction().begin();
Query query = eManager.createQuery("update Person2 p set p.name=:name ");
query.setParameter("name", "how are you");
query.executeUpdate();
eManager.getTransaction().commit();
eManager.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
create table PERSON
(
ID NUMBER(10) not null,
NAME VARCHAR2(20),
BIRTHDAY DATE,
GENDER VARCHAR2(10),
INFOR CLOB
)