@PersistanceContext,EntityManager获取null,hibernate使用jpa 2.1

时间:2017-05-02 21:15:54

标签: hibernate jpa java-ee oracle11g glassfish-4

首先,抱歉我的英语不好。 我在oracle 11g的java web应用程序中工作,使用java自动工具构建实体。调试后我注意到了null EntityManager。这是代码。

控制器:

@WebServlet(urlPatterns = {"/listarPacientes"})
public class listarPacientes extends HttpServlet {

   @EJB
   DAO.usuarioDAO servicio;

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse 
   response) throws ServletException, IOException {

    List<CesPersona> lista=servicio.listar();
        request.setAttribute("lista", lista);
        request.getRequestDispatcher("mostrarPacientes.jsp").forward(request, 
    response);        
    }
}

模型

@Stateless
public class usuarioDAO {   
    Connection con;
    @PersistenceContext(unitName = "FarmaciaPU") 
    private EntityManager em;   

    public List<CesPersona> listar() throws Exception{  
        return em.createNamedQuery("CesPersona.findAll").getResultList();//<-- this is getting null
}   

persistance xml

<?xml version="1.0" encoding="UTF-8"?>
<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="FarmaciaPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:app/farmacia/nuevo</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

glassfish 4.1 xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<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="oracle.jdbc.pool.OracleDataSource" 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="oracle-thin_XE_cesfamPool" 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="1521"/>
        <property name="databaseName" value="XE"/>
        <property name="User" value="cesfam"/>
        <property name="Password" value="cesfam"/>
        <property name="URL" value="jdbc:oracle:thin:@localhost:1521:XE"/>
        <property name="driverClass" value="oracle.jdbc.OracleDriver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="java:app/farmacia/nuevo" object-type="user" pool-name="oracle-thin_XE_cesfamPool"/>
</resources>

我在一整天都到处寻找,实施了一些&#34;解决方案&#34;但我仍然得到错误.. 请提前帮助和感谢。

1 个答案:

答案 0 :(得分:0)

数据源java:app/farmacia/nuevo的JNDI名称无效,因为java:app表示仅在应用程序部署范围内有效的JNDI名称。

使用java:global/farmacia/nuevo 可能会工作,否则只需在两个地方使用jdbc/farmacia/nuevo之类的内容。

如果仍然无效,那么Hibernate jar可能还没有提供给您的应用程序。尝试从persistence.xml文件中删除<provider>org.hibernate.ejb.HibernatePersistence</provider>行,改为使用本机提供程序。