package cn.com.Manager;
import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import cn.com.Beans.Test;
public class BaseEntityManager {
private static final Properties jpaProperties = new Properties();
private static final EntityManagerFactory factory = Persistence.createEntityManagerFactory("food",jpaProperties);
private static final ThreadLocal<EntityManager> tl = new ThreadLocal<EntityManager>();
public static EntityManager getEntityManager(){
EntityManager em = (EntityManager)tl.get();
if(em == null||em.isOpen()){
em = factory.createEntityManager();
tl.set(em);
}
return em;
}
public static void closeEntityManager(){
EntityManager em = (EntityManager)tl.get();
if (em != null)
em.close();
tl.set(null);
}
public static void main(String[] args){
EntityManager em = BaseEntityManager.getEntityManager();
BaseEntityManager entityManager=new BaseEntityManager();
//测试修改方法;
//entityManager.testmerge(em);
//测试添加方法;
entityManager.testsave(em);
//测试查询方法
//entityManager.testfind(em);
//测试删除方法
//entityManager.testdel(em);
}
//测试修改方法;
public void testmerge(EntityManager em)
{
em.getTransaction().begin();
Test tx = new Test();
tx.setId(1);
tx.setName("shxt");
em.merge(tx);
em.getTransaction().commit();
BaseEntityManager.closeEntityManager();
}
//测试添加方法;
public void testsave(EntityManager em)
{
em.getTransaction().begin();
Test ts=new Test();
ts.setName("shxt2");
em.persist(ts);
em.getTransaction().commit();
BaseEntityManager.closeEntityManager();
}
//测试查询方法
public void testfind(EntityManager em)
{
em.getTransaction().begin();
Test ts=new Test();
ts.setId(1);
ts.setName("guolei");
System.out.println("@@@@@@@@@@@@@@@"+em.find(Test.class, ts.getId()));
em.getTransaction().commit();
BaseEntityManager.closeEntityManager();
}
//测试删除方法
public void testdel(EntityManager em)
{
em.getTransaction().begin();
Test ts=new Test();
//如果直接用删除方法删除,删除的是游离态对象,在这里删除的应该是持久态对象.所以应该先查询,然后删除查询到的对象.
ts.setId(51);
Test t=em.find(Test.class, ts.getId());
em.remove(t);
em.getTransaction().commit();
BaseEntityManager.closeEntityManager();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
j2ee 持久化框架——jpa实例
共24个文件
jar:12个
xml:3个
class:2个
5星 · 超过95%的资源 需积分: 12 29 下载量 152 浏览量
2010-05-13
22:46:53
上传
评论
收藏 4.55MB RAR 举报
温馨提示
本人刚学习完JPA,自己做了个例子,希望对想学习JPA的同僚们有所帮助,也希望高手们来指点!
资源推荐
资源详情
资源评论
收起资源包目录
OpenJPA.rar (24个子文件)
OpenJPA
.project 1KB
.mymetadata 294B
WebRoot
WEB-INF
web.xml 375B
lib
geronimo-jta_1.1_spec-1.1.jar 15KB
serp-1.13.1.jar 201KB
commons-collections-3.2.jar 558KB
mysql-connector-java-3.1.11-bin.jar 430KB
msbase.jar 280KB
geronimo-jpa_3.0_spec-1.0.jar 51KB
mssqlserver.jar 66KB
dom4j.jar 307KB
commons-lang-2.3.jar 240KB
openjpa-1.0.0.jar 2.8MB
msutil.jar 58KB
portlet-api.jar 17KB
classes
cn
com
Manager
BaseEntityManager.class 3KB
Beans
Test.class 973B
META-INF
persistence.xml 988B
index.jsp 834B
META-INF
MANIFEST.MF 39B
.myeclipse
src
cn
com
Manager
BaseEntityManager.java 2KB
Beans
Test.java 561B
META-INF
persistence.xml 988B
.classpath 1KB
共 24 条
- 1
资源评论
- edoozoe2013-08-15用了一下 对初学者来说还不错
niguestme
- 粉丝: 3
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功