简体   繁体   English

实体管理器始终为null

[英]Entity Manager always null

I'm developing a J2EE application, but I've got a problem where my _entity_manager is always null . 我正在开发J2EE应用程序,但是我的_entity_manager始终为null ,这是一个问题。 I checked my code, my pool, I restarted the EJB, and I restarted the GlassFish server, but I am still having the problem. 我检查了我的代码,我的缓冲池,重新启动了EJB,然后重新启动了GlassFish服务器,但是仍然存在问题。

persistence.xml: persistence.xml中:

<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="teste" transaction-type="JTA">
      <jta-data-source>test</jta-data-source>
    </persistence-unit>
</persistence>

Resource injection line: 资源注入线:

@javax.persistence.PersistenceContext(type =
javax.persistence.PersistenceContextType.TRANSACTION, unitName = "test")
private javax.persistence.EntityManager _entity_manager;

The Pool 游泳池

The JDBC Ressource JDBC资源

Exception: 例外:

Grave: java.lang.NullPointerException at com.sun.common.util.logging.LoggingOutputStream$LoggingPrintStream.println(LoggingOutputStream.java:228) at org.apache.felix.gogo.runtime.threadio.ThreadPrintStream.println(ThreadPrintStream.java:205) at Controller.BCMS.start(BCMS.java:387) 严重:com.sun.common.util.logging.LoggingOutputStream $ LoggingPrintStream.println(LoggingOutputStream.java:228)处的org.apache.felix.gogo.runtime.threadio.ThreadPrintStream.println(ThreadPrintStream.java)上的java.lang.NullPointerException :205)在Controller.BCMS.start(BCMS.java:387)

This is line 387 which is where the error stems from: 这是第387行,该错误源于:

System.out.println(this._entity_manager);

Do you have many ideas why it is not working? 您有很多想法为什么它不起作用?

You have specified a PersistenceUnit name of "teste" but in your annotation you are looking for a unit name of "test" (which is the JNDI name of the DataSource, not the PersistanceUnit). 您已经将PersistenceUnit名称指定为“ teste”,但是在注释中,您正在寻找“ test”的单位名称(这是DataSource的JNDI名称,而不是PersistanceUnit)。

Try this: 尝试这个:

@PersistenceContext(unitName = "teste")
private EntityManager _entity_manager;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM