使用JSF,JPA和DAO.没有春天? [英] Using JSF, JPA and DAO. Without Spring?

查看:126
本文介绍了使用JSF,JPA和DAO.没有春天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到现在,我仍然在没有DAO的情况下仍与JSF和JPA一起工作.现在,我想使用DAO.但是如何在DAO类中初始化EntityManager?

public class AdresseHome {

    @PersistenceContext
    private EntityManager entityManager;

    public void persist(Adresse transientInstance) {
        log.debug("persisting Adresse instance");
        try {
            entityManager.persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }
}

我是否使用过Spring,还是有没有Spring的解决方案?

谢谢.

解决方案

如果您的容器没有为您注入EntityManager,则可以使用以下方法获得一个:

EntityManagerFactory factory;
factory = Persistence.createEntityManagerFactory("jpatest");
EntityManager em = factory.createEntityManager();

在persistence.xml中定义的单元的"jpatest"中

till now i still worked with JSF and JPA without DAOs. Now i'd like to use DAOs. But how can i initialize the EntityManager in the DAO-Classes?

public class AdresseHome {

    @PersistenceContext
    private EntityManager entityManager;

    public void persist(Adresse transientInstance) {
        log.debug("persisting Adresse instance");
        try {
            entityManager.persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }
}

Have I to use Spring or is there a solution that works without Spring?

Thanks.

解决方案

If your container doesn't inject the EntityManager for you, you can get one with:

EntityManagerFactory factory;
factory = Persistence.createEntityManagerFactory("jpatest");
EntityManager em = factory.createEntityManager();

Where "jpatest" from the unit defined in your persistence.xml

这篇关于使用JSF,JPA和DAO.没有春天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆