没有使用@PersistenceUnit注入EntityManagerFactory [英] EntityManagerFactory not being injected using @PersistenceUnit

查看:96
本文介绍了没有使用@PersistenceUnit注入EntityManagerFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java初学者.我在使用JTA事务配置持久性单元时遇到麻烦. 我需要使用已经定义,配置和填充的PostgreSQL数据库.使用netbeans,我创建了persistance.xmlglassfish-resources.xml作为休假:

I'm a java beginner. I'm in trouble to configure a persistance unit using JTA transactions. I need to use a PostgreSQL database that is already defined, configured and populated. Using netbeans, i created the persistance.xml and glassfish-resources.xml as fallows:

<persistence version="2.0" 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">
    <persistence-unit name="WellWatcherPU" transaction-type="JTA">
         <jta-data-source>WellWatcherDB</jta-data-source>
         <exclude-unlisted-classes>false</exclude-unlisted-classes>
         <properties>
             <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
             <property name="eclipselink.logging.level" value="FINE"/>
         </properties>
     </persistence-unit>
</persistence>

<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.postgresql.ds.PGSimpleDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="post-gre-sql_geowellex_geowellexPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="5432"/>
        <property name="databaseName" value="DBNAME"/>
        <property name="User" value="USER"/>
        <property name="Password" value="PASSWORD"/>
        <property name="URL" value="jdbc:postgresql://localhost:5432/DBNAME"/>
        <property name="driverClass" value="org.postgresql.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="WellWatcherDB" object-type="user" pool-name="post-gre-sql_geowellex_geowellexPool"/>
</resources>

这就是我获取EntityManagerFactory和EntityManager的方式(如在netBeans示例中使用的那样)

And this is how i get the EntityManagerFactory and EntityManager (as used in the netBeans example)

public class EUserDao {

@Resource
private UserTransaction utx = null;
@PersistenceUnit(unitName = "WellWatcherPU")
private EntityManagerFactory emf = null;

public EntityManager getEntityManager() {
    return emf.createEntityManager();  <-------- NullPointerException here
}

public EUser getOne(long userId){
    EntityManager em = getEntityManager();
    try {
        return em.find(EUser.class, userId);
    } finally {
        em.close();
    }
}

这是我的glassfish部署日志:

And here is my glassfish deploy log:

信息:[EL配置]:2012-05-10 12:01:13.534--ServerSession(2017352940)-连接(1901223982)-线程(Thread [admin-thread-pool-4848(5),5 ,grizzly-kernel])-连接(DatabaseLogin( platform =>数据库平台 用户名=>" connector => JNDIConnector数据源名称=> null ))

Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(1901223982)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--connecting(DatabaseLogin( platform=>DatabasePlatform user name=> "" connector=>JNDIConnector datasource name=>null ))

信息:[EL配置]:2012-05-10 12:01:13.534--ServerSession(2017352940)-连接(1462281761)-线程(Thread [admin-thread-pool-4848(5),5 ,grizzly-kernel])-已连接:jdbc:postgresql://localhost:5432/geowellex?loginTimeout = 0& prepareThreshold = 0 用户:geowellex 数据库:PostgreSQL版本:9.1.3 驱动程序:PostgreSQL本机驱动程序版本:带SSL的PostgreSQL 8.3 JDBC3(内部版本603)

Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(1462281761)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--Connected: jdbc:postgresql://localhost:5432/geowellex?loginTimeout=0&prepareThreshold=0 User: geowellex Database: PostgreSQL Version: 9.1.3 Driver: PostgreSQL Native Driver Version: PostgreSQL 8.3 JDBC3 with SSL (build 603)

信息:[EL配置]:2012-05-10 12:01:13.534--ServerSession(2017352940)-连接(766700859)-线程(Thread [admin-thread-pool-4848(5),5 ,grizzly-kernel])-连接(DatabaseLogin( platform => PostgreSQL平台 用户名=>" connector => JNDIConnector数据源名称=> null ))

Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(766700859)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--connecting(DatabaseLogin( platform=>PostgreSQLPlatform user name=> "" connector=>JNDIConnector datasource name=>null ))

怎么了?

推荐答案

最可能的问题是您的EUserDao只是常规类.注入仅适用于容器管理的类.对于普通类,不会处理诸如@PersistenceUnit和@Resource之类的注释.

Most likely problem is that your EUserDao is just regular class. Injection works only for container managed classes. Annotations like @PersistenceUnit and @Resource are not processed for normal classes.

以下类的类型是容器管理的类(在那些@PersistenceUnit中可以使用):

Following types of classes are container managed classes (and in those @PersistenceUnit can be used):

  • Servlet:Servlet,Servlet过滤器,事件监听器
  • JSP:标记处理程序,标记库事件侦听器
  • JSF:范围内的受管bean
  • JAX-WS:服务端点,处理程序
  • EJB:bean,拦截器
  • 托管豆:托管豆
  • CDI:CDI风格的托管bean,装饰器
  • Java EE平台:主类(静态),登录回调处理程序

这篇关于没有使用@PersistenceUnit注入EntityManagerFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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