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
<provider>
to persistence.xml, but this also did not work for meUpdate (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/
jar/META-INF/
). If I copy&paste it manually, it works. Why is that?Any help and hints are greatly appreciated.
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.
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.
The Java Persistence API allows you to define multiple persistence units, each of which can map to a separate database.
A couple of possible issues:
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
.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" />
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With