我们可以使用JPA outsite EJB容器吗?

时间:2014-11-21 09:22:09

标签: java hibernate java-ee jpa jboss7.x

我的应用程序在Jboss 4.0.5 GA上运行,需要升级才能在jboss 7.1.1 final上运行。 JSF页面中的所有内容,业务逻辑都得到了解决。但是我们面临的关键问题是:在jboss4中的旧应用程序中我们不使用JPA,我们直接使用hibernate会话。并通过threadlocal管理hibernate会话。我们应用了3层应用程序:[UI后端bean]调用[EJB执行业务]调用[DAO对象通过hibernate会话获取和更新数据库]。
这个设计现在已经破了!因为我们找不到像hibernate会话那样在threadlocal中管理实体管理器的方法。可能是我的开发人员不好。
请帮助我,我们可以使用jpa outsite EJB。我想要一个名为DAOUtil的静态类来管理和获取实体管理器,还执行保存或更新数据。有人告诉我这样做的方法。如何使用datasource(xa-datasource),persistent.xml怎么样? 这是访问数据库的类:

package com.esilicon.dao.hibernate;

import java.io.Serializable;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.engine.spi.SessionImplementor;

public class HibernateDAOSession extends DAOSession {


    /**
     * @associates Class
     */
    private static HashMap implementations = new HashMap();
    private Session hibernateSession;
    // private Transaction tx;
    private boolean JTAEnvironment;
    private EntityManager em;

    public HibernateDAOSession(Session s, boolean jta) {
        this.hibernateSession = s;
        this.JTAEnvironment = false;
    }

    public HibernateDAOSession(Session s, boolean jta, EntityManager em) {
        this.hibernateSession = s;
        this.JTAEnvironment = false;
        this.em = em;
    }

    public BaseDAO getDAO(Class clazz) {
        try {
            Class impl = getImplementationClass(clazz);
            HibernateDAO dao = (HibernateDAO) impl.newInstance();
            // dao.setCurrentSession(this.getHibernateSession());
            // session will be provided by DAOUtil from a threadlocal variable.
            return dao;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private Class getImplementationClass(Class clazz) throws ClassNotFoundException {
        Class impl = (Class) implementations.get(clazz);
        if (impl == null) {
            String name = clazz.getName();
            String packageName = clazz.getPackage().getName();
            String className = name.substring(packageName.length() + 1);
            StringBuffer implClassName = new StringBuffer(packageName).append(".hibernate.").append(className).append("DAO");
            impl = Class.forName(implClassName.toString());
            implementations.put(clazz, impl);
        }
        return impl;
    }

    public void close() {
        if (this.JTAEnvironment)
            return;
        try {
            Session s = this.getHibernateSession();
            if (s != null && s.isOpen() && this.getEm() != null) {
                s.close();
                this.setHibernateSession(null);
                /*
                 * this.getEm().clear(); this.getEm().close();
                 */
                this.setEm(null);
            }

            /*if (s != null && s.isOpen()) {

            }*/

        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }

    }

    public void beginTransaction() {
        try {
            /*if (getHibernateSession().getTransaction() == null) {
                this.getHibernateSession().beginTransaction();
            } else if (!getHibernateSession().getTransaction().isActive() || getHibernateSession().getTransaction().wasCommitted()) {
                getHibernateSession().getTransaction().begin();
            }*/
            //return getHibernateSession().getTransaction();
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public void commit() {
        //this.getHibernateSession().flush();
        /*if (this.JTAEnvironment)
            return;
        try {
            if (getHibernateSession().getTransaction() != null && getHibernateSession().getTransaction().isActive()) {
                getHibernateSession().getTransaction().commit();
            }
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            throw new HibernateDAOException(e);
        }*/
    }

    public void rollback() {
        // TODO: should we allow rolback on session in JTAEnvironment ?
        /*try {
            if (getHibernateSession().getTransaction() != null && getHibernateSession().getTransaction().isActive()) {
                getHibernateSession().getTransaction().rollback();
            }
        } finally {
            // close();
            DAOUtil.closeEntityManager();
        }*/
    }

    public Transaction getCurrentTransaction() {
        return getHibernateSession().getTransaction();
    }

    private Query prepareQuery(Query q, int start, int count, Map params) {
        String[] namedParams = q.getNamedParameters();
        if (namedParams.length > 0 && params != null) {
            for (int i = 0; i < namedParams.length; i++) {
                if (params.get(namedParams[i]) instanceof java.util.List) {
                    q.setParameterList(namedParams[i], (List) params.get(namedParams[i]));
                } else {
                    q.setParameter(namedParams[i], params.get(namedParams[i]));
                }
            }
        }
        if (start >= 0)
            q.setFirstResult(start);
        if (count >= 0)
            q.setMaxResults(count);
        return q;
    }

    public Session getHibernateSession() {
        return hibernateSession;
    }

    public void setHibernateSession(Session hibernateSession) {
        this.hibernateSession = hibernateSession;
    }

    public Object update(Object o) {
        try {
            this.getHibernateSession().update(o);
            return o;
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }

    }

    public boolean isJTAEnvironment() {
        return JTAEnvironment;
    }

    public Object save(Object o) {
        try {
            this.getHibernateSession().save(o);
            return o;
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public Object saveOrUpdate(Object o) {
        try {
            this.getHibernateSession().saveOrUpdate(o);
            return o;
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }

    }

    public void flush() {
        try {
            this.getHibernateSession().flush();
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public Object refresh(Object o) {
        try {
            this.getHibernateSession().refresh(o);
            return o;
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public void evict(Object o) {
        try {
            this.getHibernateSession().evict(o);
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public Object query(String query, Map map, boolean uniqueResult) {
        try {
            Query q = this.getHibernateSession().createQuery(query);
            prepareParameters(q, map);
            if (uniqueResult) {
                return q.uniqueResult();
            } else {
                return q.list();
            }
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    private void prepareParameters(Query q, Map map) {
        if (map == null)
            return;
        String[] params = q.getNamedParameters();
        for (int i = 0; i < params.length; i++) {
            if (map.get(params[i]) instanceof java.util.List) {
                q.setParameterList(params[i], (List) map.get(params[i]));
            } else {
                q.setParameter(params[i], map.get(params[i]));
            }
        }
    }

    public Connection getConnection() {
        SessionImplementor si = (SessionImplementor) getHibernateSession();
        //this.connection = si.connection();
        return si.connection();
    }

    public void delete(Object o) {
        try {
            this.getHibernateSession().delete(o);
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }

    }

    public Object findUniqueEntityByID(Class entityClass, Serializable id) {
        try {
            return this.getHibernateSession().get(entityClass, id);
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public int executeUpdate(String query, Map params) {
        try {
            Query q = this.getHibernateSession().createQuery(query);
            prepareParameters(q, params);
            return q.executeUpdate();
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public Object nativeQuery(String query, Map map, boolean uniqueResult) {
        try {
            Query q = this.getHibernateSession().createSQLQuery(query);
            prepareParameters(q, map);
            if (uniqueResult) {
                return q.uniqueResult();
            } else {
                return q.list();
            }
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public int executeNativeUpdate(String query, Map params) {
        try {
            Query q = this.getHibernateSession().createSQLQuery(query);
            prepareParameters(q, params);
            return q.executeUpdate();
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new HibernateDAOException(e);
        }
    }

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public Query createQuery(String hqlQuery) {
        return getHibernateSession().createQuery(hqlQuery);
    }

    @Override
    public SQLQuery createSQLQuery(String nativeQuery) {
        // TODO Auto-generated method stub
        return getHibernateSession().createSQLQuery(nativeQuery);
    }

    @Override
    public void disconnect() {
        // TODO Auto-generated method stub
        getHibernateSession().disconnect();
    }

    @Override
    public Object get(Class<?> clazz, Serializable id) {
        // TODO Auto-generated method stub
        return getHibernateSession().get(clazz, id);
    }

    @Override
    public Object load(Class<?> clazz, Serializable id) {
        // TODO Auto-generated method stub
        return getHibernateSession().load(clazz, id);
    }

    @Override
    public Object merge(Object o) {
        // TODO Auto-generated method stub
        return getHibernateSession().merge(o);
    }

    @Override
    public Query getNamedQuery(String nameQuery) {
        // TODO Auto-generated method stub
        return getHibernateSession().getNamedQuery(nameQuery);
    }
}

管理HibernateSessionDAO的类

package com.esilicon.dao;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.proxy.HibernateProxy;

import com.esilicon.dao.hibernate.HibernateDAOSession;
import com.esilicon.util.logging.Logger;

public class DAOUtil {
    public static final String PERSISTENT_UNIT = "vms4_jboss7";

    static ThreadLocal<DAOSession> threadLocalDAOEntityManager = new ThreadLocal<DAOSession>();
    static Logger logger = Logger.getLogger(DAOUtil.class.getName());

    public static DAOSession openEntityManager(DAOEntityManagerFactory factory) {
        DAOSession entityManager = (DAOSession) threadLocalDAOEntityManager.get();
        if (entityManager != null && entityManager.isOpen()) {
            return entityManager;
        }
        try {
            entityManager = factory.getEntityManager();
        } catch (Exception e) {
            // e.printStackTrace();
            entityManager = factory.openEntityManager();
        }
        threadLocalDAOEntityManager.set(entityManager);
        return entityManager;
    }

    public static DAOSession openSession(DAOSessionFactory factory) {

        DAOSession daoSession = (DAOSession) threadLocalDAOEntityManager.get();
        if (daoSession != null && daoSession.isOpen()) {
            return daoSession;
        }
        try {
            threadLocalDAOEntityManager.remove();
            daoSession = factory.getSession();
        } catch (Exception e) {
            e.printStackTrace();
            daoSession = factory.openSession();
        }
        threadLocalDAOEntityManager.set(daoSession);
        return daoSession;
    }

    public static DAOSession openSession(DAOEntityManagerFactory factory) {

        return openEntityManager(factory);
    }

    public static DAOSession getEntityManager() {
        DAOSession entityManager = (DAOSession) threadLocalDAOEntityManager.get();
        if (entityManager == null || !entityManager.isOpen()) {
            entityManager = DAOUtil.openEntityManager(DAOEntityManagerFactory.getInstance(null));
        }
        return entityManager;
    }

    public static DAOSession getSession() {
        return getEntityManager();
    }

    public static void closeEntityManager() {
        DAOSession entityManager = (DAOSession) threadLocalDAOEntityManager.get();
        if (entityManager != null) {
            entityManager.close();
            logger.debug("EntityManager Closed for Thread " + Thread.currentThread().getName());
            threadLocalDAOEntityManager.remove();
        }
    }

    public static void closeSession() {
        closeEntityManager();
    }

    public static void beginTransaction() {
        DAOSession entityManager = (DAOSession) threadLocalDAOEntityManager.get();
        logger.debug("--- BeginTransaction --- Thread " + Thread.currentThread().getName());
        if (entityManager == null) {
            entityManager = openEntityManager(DAOEntityManagerFactory.getInstance(null));
        }
        entityManager.beginTransaction();
    }

    public static void commitTransaction() {
        DAOSession entityManager = (DAOSession) threadLocalDAOEntityManager.get();
        logger.debug("--- CommitTransaction --- Thread " + Thread.currentThread().getName());
        entityManager.commit();
    }

    public static void rollbackTransaction() {
        DAOSession entityManager = (DAOSession) threadLocalDAOEntityManager.get();
        if (entityManager != null && entityManager.isOpen()) {
            entityManager.rollback();
        }
    }

    public static BaseDAO getDAO(Class clazz) {
        return getEntityManager().getDAO(clazz);
    }

    public static Object narrow(Object proxy) {

        try {
            return ((HibernateProxy) proxy).getHibernateLazyInitializer().getImplementation();
        } catch (Exception e) {
            return proxy; // not a proxy
        }

    }

    public static void closeConnection(Connection con) {
        try {
            if (con != null && !con.isClosed()) {
                con.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static Connection getNonXAConnection() {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup(JNDILookup.PURE_CONNECTION_JNDI);
            return dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
            logger.fatal(e.getMessage(), e.getCause());
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

使用依赖注入将实体管理器注入您的dao类:

例如:

@PersistenceContext
private EntityManager entityManager;