Why is persistence unit null when persistence.xml exists

I am using Wildfly 8.1 along with EJB Project objects (EJB 3.2) containing objects. When I try to inject 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 am trying to enter an EntityManager:

 @Stateless public class MitarbeiterDAO implements MitarbeiterDAORemote { @PersistenceContext private EntityManager em; public int writeMitarbeiterToDB(Mitarbeiter m) { em.persist(m); return m.getId(); } } 

I specified the following persistence.xml file, which I placed in "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> 

Related issues and solutions

  • Cannot find persistence block suggests persistence.xml file included
  • An entityManager injection in Wildfly / Jboss indicates that there may be a different directory to which my persistence.xml belongs. I tried this directory but still got the same error message.
  • Cannot find persistence block from persistence.xml Although this is not quite the same question, the solution was to add a <provider> to persistence.xml, but this also does not work for me

Update (see comments):
- I tried using @EntityManager(unitName="mitarbeiter") instead, just @EntityManager - The Bean session shown above is the only place I try to enter EntityManager

Update 2 (see comments for the answer):

  • persistence.xml is located in the directory Project/ejbModule/META-INF/
  • This directory is included in the build path.
  • Somehow it does not deploy, and other files in the same directory (up to jar/META-INF/ ). If I copy and paste it manually, it works. Why is this?

Any help and advice is appreciated.

+8
java wildfly entitymanager
source share
1 answer

Some possible problems:

  • Invalid persistence.xml file location. Place it in the META-INF/ at the root of the archive. If you use maven, it will be src/main/resources/META-INF/persistence.xml .
  • Invalid persistence.xml file: you have a persistence-unit tag closed in the line <persistence-unit name="mitarbeiter"/> (please note / at the end)
  • <property-name="hibernate.hbm2ddl.auto" value="create-drop"/> not valid. I suppose you wanted: <property name="hibernate.hbm2ddl.auto" value="create-drop" />
  • You are also using an outdated provider.

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

 <?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> 
+6
source share

All Articles