我是一名java初学者。我很难使用JTA事务配置持久性单元。
我需要使用已经定义,配置和填充的PostgreSQL数据库。使用netbeans,我创建了persistance.xml
和glassfish-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) - 连接(1901223982) - 线程(线程[admin-thread-pool-4848(5),5 ,灰色内核]) - 连接(DatabaseLogin( 平台=&GT; DatabasePlatform 用户名=&gt; “” connector =&gt; JNDIConnector datasource name =&gt; null ))
信息:[EL配置]:2012-05-10 12:01:13.534 - ServerSession(2017352940) - 连接(1462281761) - 线程(线程[admin-thread-pool-4848(5),5 ,grizzly-kernel]) - 已连接:jdbc:postgresql:// localhost:5432 / geowellex?loginTimeout = 0&amp; prepareThreshold = 0 用户:geowellex 数据库:PostgreSQL版本:9.1.3 驱动程序:PostgreSQL本机驱动程序版本:带有SSL的PostgreSQL 8.3 JDBC3(版本603)
信息:[EL配置]:2012-05-10 12:01:13.534 - ServerSession(2017352940) - 连接(766700859) - 线程(线程[admin-thread-pool-4848(5),5 ,灰色内核]) - 连接(DatabaseLogin( 平台=&GT; PostgreSQLPlatform 用户名=&gt; “” connector =&gt; JNDIConnector datasource name =&gt; null ))
怎么了?
答案 0 :(得分:4)
最有可能的问题是你的EUserDao只是普通班级。注入仅适用于容器管理类。正常类不处理@PersistenceUnit和@Resource等注释。
以下类型的类是容器管理类(在那些@PersistenceUnit中可以使用):
答案 1 :(得分:1)
我在你的代码中看到了声明:
private EntityManagerFactory emf = null;
但永远不会创建一个......像这样
emf = Persistence.createEntityManagerFactory("WellWatcherPU");
这就是为什么在使用对象时会出现Null指针异常的原因!
public EntityManager getEntityManager() {
return emf.createEntityManager(); <-------- NullPointerException here
}