1

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

<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 示例中使用)

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 部署日志:

信息:[EL 配置]: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)--Connection(1462281761)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly- kernel])--连接:jdbc:postgresql://localhost:5432/geowellex?loginTimeout=0&prepareThreshold=0 用户:geowellex 数据库:PostgreSQL 版本:9.1.3 驱动程序:PostgreSQL 本机驱动程序版本:PostgreSQL 8.3 JDBC3 with SSL (build 603 )

信息:[EL 配置]: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用户名=>""连接器=>JNDIConnector数据源名=>null))

怎么了?

4

2 回答 2

4

最可能的问题是您的 EUserDao 只是普通课程。注入仅适用于容器管理的类。普通类不会处理 @PersistenceUnit 和 @Resource 之类的注释。

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

  • Servlet:servlet、servlet 过滤器、事件监听器
  • JSP:标签处理程序、标签库事件监听器
  • JSF:作用域托管 bean
  • JAX-WS:服务端点、处理程序
  • EJB:bean,拦截器
  • 托管bean:托管bean
  • CDI:CDI 风格的托管 bean、装饰器
  • Java EE 平台:主类(静态)、登录回调处理程序
于 2012-05-10T15:13:54.037 回答
2

我在您的代码声明中看到:

private EntityManagerFactory emf = null;

但永远不要创造一个......像这样

emf = Persistence.createEntityManagerFactory("WellWatcherPU");

这就是为什么在使用对象时会出现空指针异常!

public EntityManager getEntityManager() {
    return emf.createEntityManager();  <-------- NullPointerException here
}
于 2013-02-28T23:37:56.047 回答