7

我将 JSF2 与纯 JPA2 一起使用。但问题在于 entityManager,

@PersistenceContext
private EntityManager entityManager;

这里 entityManager 没有被注入并且始终为空。有人可以帮我看看我的代码有什么问题吗?

这是我的配置。

用户.java

    @Entity
    @Table(name="USER")
    public class User {

        private int id;
        private String name;
        private String surname;

        @Id
        @SequenceGenerator(name="user_id_seq_gen", sequenceName="USER_ID_GEN_SEQ", allocationSize=1)
        @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="user_id_seq_gen")
        @Column(name="ID", unique = true, nullable = false)
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Column(name="NAME", unique = true, nullable = false)
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Column(name="SURNAME", unique = true, nullable = false)
        public String getSurname() {
            return surname;
        }

        public void setSurname(String surname) {
            this.surname = surname;
        }   

    }

持久性.xml

   <?xml version="1.0" encoding="UTF-8"?>
        <persistence xmlns="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 
                     http://java.sun.com  /xml/ns/persistence/persistence_2_0.xsd" version="2.0">
           <persistence-unit name="testUnit" transaction-type="RESOURCE_LOCAL">
              <provider>org.hibernate.ejb.HibernatePersistence</provider>
              <non-jta-data-source>java:/comp/env/testDS</non-jta-data-source>
              <class>com.user.external.test.User</class>
              <exclude-unlisted-classes>false</exclude-unlisted-classes>
              <properties>
                 <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
                 <property name="hibernate.max_fetch_depth" value="6"/>
                 <property name="hibernate.show_sql" value="false"/>
                 <property name="hibernate.format_sql" value="true"/>
              </properties>
           </persistence-unit>
        </persistence>

Tomcat 服务器.xml

    <Context docBase="usertools" path="/usertools" reloadable="true">
        <ResourceLink name="testDS" 
                  global="testDS"  
                  type="javax.sql.DataSource"/>
    </Context> 

TestBean.java

    @ManagedBean(name="testBean")
    @ViewScoped
    public class TestBean implements Serializable{

        @ManagedProperty("#{testService}")
        private ITestService testService;

        public void saveData(User user){
            testService.saveUser(user);
        }

        public void setTestService(ITestService testService){
            this.testService = testService;
        }

        public ITestService getTestService(){
            return testService;
        }
    }

TestServiceImpl.java

    @ManagedBean(name="testService")
    @ApplicationScoped
    public class TestServiceImpl implements ITestService {

        @ManagedProperty(value="#{testDAO}")
        private ITestDAO testDAO;

        public void saveUser(User user){
            testDAO.saveUser(user);
        }

        public void setTestDAO(ITestDAO testDAO){
            this.testDAO = testDAO;
        }

        public ITestDAO getTestDAO(){
            return testDAO;
        }

    }   

TestDao.java

    @ManagedBean(name = "testDAO")
    @ApplicationScoped
    public class TestDAO implements ITestDAO {

        @PersistenceContext
        private EntityManager entityManager; // is null

        public void saveUser(User user){
            entityManager.persist(user); // Getting Null Pointer Here....
        }   

        public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
        }

        public EntityManager getEntityManager() {
            return entityManager;
        }

    }

我尝试过使用 @PersistenceContext 名称和 unitName 属性。但它仍然无法正常工作。还尝试使用 web.xml 中的资源配置

    <persistence-context-ref>
            <persistence-context-ref-name>testUnit</persistence-context-ref-name>
            <persistence-unit-name>testUnit</persistence-unit-name>
        </persistence-context-ref>

        <resource-ref>
            <description>DB Connection</description>
            <res-ref-name>testUnit</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
        </resource-ref>

仍然没有运气。有人可以帮帮我吗。

4

1 回答 1

13

Tomcat 不是 Java EE 容器,因此存在与容器管理工厂和依赖注入相关的限制。其中,您需要手动创建EntityManagerFactoryEntityManager.

Hibernate 文档对此并不清楚,所以这里是 Eclipselink 之一:Tomcat/JPA 教程。检查“对 JPA 的限制”部分,这同样适用于 Hibernate。

也可以看看:

于 2012-06-29T12:34:56.073 回答