0

first of all, sorry for my bad english. Im working in a java web app with oracle 11g, using the java automatic tool to construct the entities. After debugging i noticed the null EntityManager. Here is the code.

Controller:

@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);        
    }
}

Model

@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>

i really looked everywhere during all day, implemented some "solutions" but i still getting the error.. please help and thanks in advance.

1 Answers1

0

The JNDI name for your data source java:app/farmacia/nuevo is invalid because java:app indicates a JNDI name that is only valid from inside the scope of an application's deployment.

Using java:global/farmacia/nuevo might work, otherwise just use something like jdbc/farmacia/nuevo in both places.

If that still does not work then perhaps the Hibernate jars have not been made available to your application. Try removing the <provider>org.hibernate.ejb.HibernatePersistence</provider> line from your persistence.xml file and use the native provider instead.

Steve C
  • 18,876
  • 5
  • 34
  • 37