Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get persistence to work in JBoss AS 7 ("Can't find a deployment unit named at subdeployment")

I try to implement a demo that uses JBoss AS 7 with default hibernate and default Database shipped with JBoss AS. My implementation is similar to the "login" example from JBoss (I have some extra projects to implement a web service and deploy as EAR) and I can't catch the difference. Anyway I constantly get Runtime Exceptions such as

Caused by: java.lang.RuntimeException: Can't find a deployment unit named  at subdeployment "myproject.persistence.war" of deployment "myproject.ear"
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:94) [jboss-as-weld-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.resolvePersistenceContext(WeldJpaInjectionServices.java:59) [jboss-as-weld-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.weld.util.Beans.injectEEFields(Beans.java:784) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

My MANIFEST.MF

Manifest-Version: 1.0
Dependencies: org.hibernate

My beans.xml is empty. In my lib folder I have the hibernate jars to have access to native hibernate features.

My persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="accessControlDatabase">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>
</persistence>  

And my two relevant classes:

import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class Resources {
    @SuppressWarnings("unused")
    @Produces
    @PersistenceContext(name = "accessControlDatabase")
    private EntityManager em;
}

import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import myproject.model.Session;
import myproject.model.User;

@Named("accessControlRepository")
@RequestScoped
public class AccessControlRepository {
    @Inject
    private EntityManager em;

    @SuppressWarnings("unchecked")
    public List<Session> getAllSessions() {
        return em.createQuery("from Session").getResultList();
    }
}

What can cause the exception? What should I look for to resolve the problem?

like image 340
Arne Deutsch Avatar asked Nov 18 '11 08:11

Arne Deutsch


People also ask

Where is persistence xml Jboss?

It has to be included in the META-INF directory inside the JAR file that contains the entity beans. The persistence. xml file must define a persistence-unit with a unique name in the current scoped classloader.

What is persistence unit JPA?

A persistence unit defines the details that are required when you acquire an entity manager. To package your EclipseLink JPA application, you must configure the persistence unit during the creation of the persistence. xml file. Define each persistence unit in a persistence-unit element in the persistence.

Where is persistence xml created?

Create a persistence. xml file that resides in the META-INF folder. Another option is to set the packagesToScan property in Spring's config, like this: <code> <bean id="entityManagerFactory" class="org. springframework.

Can we have multiple persistence xml?

xml files. Have multiple persistence units in one persistence.


1 Answers

I have found a solution. I have to put the persistence.xml into the META-INF folder of the EAR project. If I do so I can use the @PersistenceContext annotation to get at the EntityManager. I think it should work the other way, too. This seems to be a workaround, not a real solution.

like image 77
Arne Deutsch Avatar answered Nov 14 '22 21:11

Arne Deutsch