I'm setting up a java EE project using (open)JPA. I'm using glassfish 4.0 as my application server, but can't seem to get the persistence to work.

The problem I'm facing seems to be a rather general issue, since the answers and solutions to the same error differ a lot, the error seems to be caused by a lot of different conditions. I've tried most of the solutions I found, unfortunately with no avail.

I'm trying to inject the EntityManager in one of my DAO's

@Singleton
@Stateless
public class UserDAOJPAImpl implements UserDAO
{

    @PersistenceContext(unitName = "pu")
    private EntityManager em;

The persistence.xml is using openJPA as provider, and should connect to my JDBC resource in Glassfish, which uses MySQL. The configuration appears to be correct, since the ping tests succeed.

My persistence.xml looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="pu" transaction-type="JTA">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>jdbc/Kwetter</jta-data-source>
        <class>kwetter.domain.Tweet</class>
        <class>kwetter.domain.User</class>
    </persistence-unit>
</persistence>

The persistence.xml file is located under src/main/java/META-INF/persistence.xml

Everything seems to inject just fine, however. When I start executing a query using the the EntityManager, I get the following error:

java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName pu
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:158)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:281)
    at kwetter.dao.UserDAOCollectionImpl.create(UserDAOCollectionImpl.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    ... 84 more
]]

I've gone through all my settings multiple times but I just can't seem to find the cause for this issue. Any help would be greatly appreciated.

有帮助吗?

解决方案

As pL4Gu33 suggested in the comments, the location of my persistence.xml appeared to be incorrect.

My IDE (IntelliJ IDEA) automatically puts the persistence.xml file under a META-INF folder. However, I needed to have the META-INF folder under a resources folder in my project.

The persistence.xml file is loaded after changing the location of my persistence.xml file: src/main/resources/META-INF/persistence.xml.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top