使用jpa jdk必需在1.5及以上
数据源文件 必需放在src的META-INF文件夹下
数据源文件配置 数据库采用oracle数据库
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns:persistence="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 persistence_1_0.xsd ">
<persistence-unit name="leamJPA" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.username" value="userName" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
持久化对象类
@Entity
@Table(name="tb_person")
public class Person implements java.io.Serializable {
private static final long serialVersionUID = 1L;
//必需要有id主键 指定PERSONSEQ序列
@Id @Column(name="id") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="PERSONSEQ",allocationSize=1)
private Integer id;
@Column(name="name",length=30,nullable=false)
private String name;
@Column(name="addTime")
public Person(){}
/**
* @return the jobtypeid
*/
public Integer getId() {
return id;
}
/**
* @param jobtypeid the jobtypeid to set
*/
public void setJId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
创建实体管理器获取数据源
public class JPAUtil {
private static EntityManagerFactory emf;
static{
emf=Persistence.createEntityManagerFactory("leamJPA");
}
public static EntityManager getEntityManager(){
return emf.createEntityManager();
}
}
测试类 数据库不用建表 会自动创建实体对象类对应的表结构
public class PersonTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test//测试持久
public void testSave(){
EntityManager emf=JPAUtil.getEntityManager();
try{
EntityTransaction transaction=emf.getTransaction();
transaction.begin();
Person ps=new Person();
ps.setAddTime(new Date());
ps.setFlag(1);
ps.setName("jpa测试");
ps.setOpcode("admin");
emf.persist(ps);
transaction.commit();
}finally{
if(emf!=null)emf.close();
}
}
@Test//测试更新
public void testUpdate(){
EntityManager emf=JPAUtil.getEntityManager();
try{
EntityTransaction transaction=emf.getTransaction();
transaction.begin();
Person ps=emf.getReference(Person.class, 2);
ps.setAddTime(new Date());
ps.setFlag(1);
ps.setName("jpa测试更新222");
ps.setOpcode("admin");
emf.merge(ps);
transaction.commit();
}
finally{
if(emf!=null)emf.close();
}
}
@Test//测试获取对象
public void testGetPerson(){
EntityManager emf=JPAUtil.getEntityManager();
try{
Person ps=emf.find(Person.class, 2);
System.out.println(ps.getName());
}
finally{
if(emf!=null)emf.close();
}
}
@Test//测试获取多个对象
public void testGetPersonList(){
EntityManager emf=JPAUtil.getEntityManager();
try{
Query query=emf.createQuery("select p from Person p where id=:id");
query.setParameter("id", 3);
@SuppressWarnings("unchecked")
List<Person> list=query.getResultList();
for(Person p:list){
System.out.println(p.getId());
System.out.println(p.getName());
}
}
finally{
if(emf!=null)emf.close();
}
}
@Test//测试删除
public void testRemove(){
EntityManager emf=JPAUtil.getEntityManager();
try{
EntityTransaction transaction=emf.getTransaction();
transaction.begin();
Person ps=emf.getReference(Person.class, 2);
emf.remove(ps);
transaction.commit();
}
finally{
if(emf!=null)emf.close();
}
}
}
分享到:
相关推荐
Open JPA2 employee 简单例子
Spring使用JavaConfig集成JPA简单例子,对应博文地址为http://blog.csdn.net/wu_boy/article/details/72353608
Spring集成JPA和MyBatis简单例子
Spring集成JPA和MyBatis简单例子
Spring4集成JPA和MyBatis3简单例子
NULL 博文链接:https://ghl116.iteye.com/blog/1144003
jpa和hibernate的一个简单例子
jpa + hibernate 简单列子实现和junit测试 完整jar包都已测试过。
NULL 博文链接:https://chentian114.iteye.com/blog/2287028
清单2在容器中运行的JPA例子 5 3. Entity Bean 6 3.1定义对Entity中属性变量的访问 6 3.2主键和实体标识(Primary Key and Entity Identity) 8 4. EntityManager 9 4.1配置和获得EntityManager 9 4.2 Entity的生命...
在学习jpa时候做的一个struts2+spring+jpa整合的完整例子 包含分页,一个简单的 资产入库系统 并实现了登陆等。
springMvc+spring+spring-data-jpa maven整合例子,附带简单逻辑实现代码。
jbossas7及seamremoting,jpa hibernate搭建的简单小例子
清单2在容器中运行的JPA例子 5 3. Entity Bean 6 3.1定义对Entity中属性变量的访问 6 3.2主键和实体标识(Primary Key and Entity Identity) 8 4. EntityManager 9 4.1配置和获得EntityManager 9 4.2 Entity的生命...
jbossas7及seamremoting,jpa hibernate搭建的简单小例子
清单2在容器中运行的JPA例子 5 3. Entity Bean 6 3.1定义对Entity中属性变量的访问 6 3.2主键和实体标识(Primary Key and Entity Identity) 8 4. EntityManager 9 4.1配置和获得EntityManager 9 4.2 Entity的生命...
JPA对于单表的或者简单的SQL查询非常友好,甚至可以说非常智能。他为你准备好了大量的拿来即用的持久层操作方法。甚至只要写findByName这样一个接口方法,他就能智能的帮你执行根据名称查找实体类对应的表数据,完全...
一个非常简单易懂的 JPA 例子,帮你快速入门JPA
maven+springmvc+springjpa+hibernate简单例子,有需要的朋友可以参考看看,没费的哦。