Question

I am trying to update a small alpha version of a enterprise application originally written in Java6. Now I want to use:

  • Java 7
  • JSF latest
  • Maven
  • EJB 3.2 with Glassfish

So far I can deploy my EAR file on Glassfish without problems. My webapp can be loaded, the first JSF pages navigate fine. And my JSF backing bean seems to also load my Stateless session beans fine. Debugging showed me, I can get from one SSB to another coming from my backing bean. The last and final step that I am missing is my entity manager and persistence.

My class is annoated with @Stateless and i am using:

@PersistenceContext(unitName = "myProjectPU")
    protected EntityManager entityManager;

But the entity manager is null :(

My persistence.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="myProjectPU" transaction-type="JTA">
  <jta-data-source>jdbc/myProject</jta-data-source>
    <properties>
      <property name="javax.persistence.schema-generation.create-database-schemas" value="true" />
    </properties>
    </persistence-unit>
  </persistence>

Any ideas? Does the persistence.xml have to be inside the ear maven module? Right now its in the ejb maven module, where my classes which use the entity manager reside.

Was it helpful?

Solution

Well finally I found the problem. Scrolling up in the glassfish logs showed that the nullpointer exception followed up an earlier exception that stated "No database selected". The problem was not in the code, but in the Glassfish JDBC Connection that I made. My ping worked fine and i thought the connection was ok. But you dont only have to add the mysql port and username and password. You also MUST change the default URL and add (additional properties in connection pool) the following value:

jdbc:mysql://localhost:3306/yourdatabase

before it was defaulted to: jdbc:mysql://:3306/

Take also care that there are two parameters, Url and URL! After that not only the ping succeeded, but also the database connection with entity manager worked fine. :(

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top