您的位置:首页 > 服装鞋帽 > 休闲装 > EJB3学习比较-JPA调用

EJB3学习比较-JPA调用

luyued 发布于 2011-01-28 22:53   浏览 N 次  

1.1.1 JPA调用

1.1.1.1 J2SE

publicclass EntityManagerHelper {

privatestaticfinal EntityManagerFactory emf;

privatestaticfinal ThreadLocal threadLocal;

privatestaticfinal Logger logger;

static {

emf = Persistence.createEntityManagerFactory("JPA_J2SEPU");

threadLocal = new ThreadLocal();

logger = Logger.getLogger("JPA_J2SEPU");

logger.setLevel(Level.ALL);

}

publicstatic EntityManager getEntityManager() {

EntityManager manager = threadLocal.get();

if (manager == null || !manager.isOpen()) {

manager = emf.createEntityManager();

threadLocal.set(manager);

}

return manager;

}

publicstaticvoid closeEntityManager() {

EntityManager em = threadLocal.get();

threadLocal.set(null);

if (em != null) em.close();

}

publicstaticvoid beginTransaction() {

getEntityManager().getTransaction().begin();

}

publicstaticvoid commit() {

getEntityManager().getTransaction().commit();

}

publicstaticvoid rollback() {

getEntityManager().getTransaction().rollback();

}

publicstatic Query createQuery(String query) {

return getEntityManager().createQuery(query);

}

publicstaticvoid log(String info, Level level, Throwable ex) {

logger.log(level, info, ex);

}

}

package com.syax.study.hibernate.jpa;

import java.util.List;

import java.util.logging.Level;

import javax.persistence.EntityManager;

import javax.persistence.Query;

图文资讯
广告赞助商