Search code examples
javawildflyentitymanager

Why is persistence unit named null when persistence.xml exists


I am using Wildfly 8.1 together with a EJB Project (EJB 3.2) containing entities. When trying to inject the Entity Manager into one of my Beans i get the following:

JBAS011440: Can't find a persistence unit named null in deployment \"EntitiesProject.jar\""},
"JBAS014771: Services with missing/unavailable dependencies" => [
    "jboss.deployment.unit.\"EntitiesProject.jar\".weld.weldClassIntrospector is missing [jboss.deployment.unit.\"EntitiesProject.jar\".beanmanager]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.HandleDelegate is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.InstanceName is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.InAppClientContainer is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.Validator is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.ORB is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.ValidatorFactory is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]"
]

This is my SessionBean where I try to inject the EntityManager:

@Stateless
public class MitarbeiterDAO implements MitarbeiterDAORemote {

    @PersistenceContext
    private EntityManager em;

    public int writeMitarbeiterToDB(Mitarbeiter m)
    {
        em.persist(m);
        return m.getId();
    }
}

I have specified the following persistence.xml file which I put to "project-name"/ejbModule/META-INF.

<persistence-unit name="mitarbeiter">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
   <properties>
      <property-name="hibernate.hbm2ddl.auto" value="create-drop"/>
   </properties>
</persistence-unit>

Similar questions and their solutions

Update (see comments):
- I have tried to use @EntityManager(unitName="mitarbeiter") instead of just @EntityManager - The Session Bean shown above is the only place where I try to inject EntityManager

Update 2 (see comments of answer):

  • persistence.xml is in the directory Project/ejbModule/META-INF/
  • This directory is included in the Build Path
  • Somehow it's not getting deployed while other files in the same directory are (to jar/META-INF/). If I copy&paste it manually, it works. Why is that?

Any help and hints are greatly appreciated.


Solution

  • A couple of possible issues:

    • Incorrect location of the persistence.xml file. Please place it inside META-INF/ directory at the root of the archive. If you use maven it would be src/main/resources/META-INF/persistence.xml.
    • Invalid persistence.xml file: you have persistence-unit tag closed in line <persistence-unit name="mitarbeiter"/> (please notice / at the end)
    • <property-name="hibernate.hbm2ddl.auto" value="create-drop"/> is invalid. I suppose you wanted: <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    • also provider you are using is deprecated.

    In general, the correct persistence.xml content for JPA 2.1 in your case should look like:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence 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"
      version="2.1">
    
      <persistence-unit name="mitarbeiter">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
    
        <properties>
          <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
      </persistence-unit>
    </persistence>