Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • Can't find persistence unit suggests to include a persistence.xml
  • Injecting entityManager in Wildfly / Jboss points out that there might be another directory where my persistence.xml belongs to. I tried that directory but still got the same error message.
  • Cannot find persistence unit from persistence.xml While this is not exactly the same question the solution was to add a <provider> to persistence.xml, but this also did not work for me

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.

like image 864
Jbartmann Avatar asked Oct 19 '15 14:10

Jbartmann


People also ask

What is persistence unit in persistence xml?

A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store.

How does persistence xml work?

The persistence. xml file must define a persistence-unit with a unique name in the current scoped classloader. The provider attribute specifies the underlying implementation of the JPA EntityManager. In JBoss AS, the default and only supported / recommended JPA provider is Hibernate.

Can we have multiple persistence xml?

The Java Persistence API allows you to define multiple persistence units, each of which can map to a separate database.


1 Answers

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>
like image 135
G. Demecki Avatar answered Nov 02 '22 19:11

G. Demecki