Tuesday, February 16, 2010

"Error: Could not open JPA EntityManager" problem on EclipseLink

When you use JPA and EclipseLink 2.0 and get this error:

HTTP Status 500 -


type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [_persistence_setperson_vh] or [_persistence_getperson_vh] is not defined in the object [com.googlecode.pu1.domain.PersonDetail].
Internal Exception: java.lang.NoSuchMethodException: com.googlecode.pu1.domain.PersonDetail._persistence_getperson_vh()
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[person]
Descriptor: RelationalDescriptor(com.googlecode.pu1.domain.PersonDetail --> [DatabaseTable(PERSONDETAIL)])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException

 javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
root cause
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [_persistence_setperson_vh] or [_persistence_getperson_vh] is not defined in the object [com.googlecode.pu1.domain.PersonDetail].
Internal Exception: java.lang.NoSuchMethodException: com.googlecode.pu1.domain.PersonDetail._persistence_getperson_vh()
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[person]
Descriptor: RelationalDescriptor(com.googlecode.pu1.domain.PersonDetail --> [DatabaseTable(PERSONDETAIL)])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException

 org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:382)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:336)
 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:102)
 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
 $Proxy22.findAll(Unknown Source)
 com.googlecode.sandcode.helloworld.HelloWorld.getName(HelloWorld.java:29)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 java.lang.reflect.Method.invoke(Method.java:597)
 javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
 javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
 com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
 com.sun.el.parser.AstValue.getValue(AstValue.java:116)
 com.sun.el.parser.AstValue.getValue(AstValue.java:163)
 com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
 com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
 javax.faces.component.UIOutput.getValue(UIOutput.java:168)
 com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:338)
 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
 javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620)
 javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
 javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
 com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
 com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
 com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
root cause
javax.persistence.PersistenceException: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [_persistence_setperson_vh] or [_persistence_getperson_vh] is not defined in the object [com.googlecode.pu1.domain.PersonDetail].
Internal Exception: java.lang.NoSuchMethodException: com.googlecode.pu1.domain.PersonDetail._persistence_getperson_vh()
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[person]
Descriptor: RelationalDescriptor(com.googlecode.pu1.domain.PersonDetail --> [DatabaseTable(PERSONDETAIL)])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException

 org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:392)
 org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151)
 org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207)
 org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195)
 org.springframework.orm.jpa.JpaTransactionManager.createEntityManagerForTransaction(JpaTransactionManager.java:400)
 org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:321)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:336)
 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:102)
 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
 $Proxy22.findAll(Unknown Source)
 com.googlecode.sandcode.helloworld.HelloWorld.getName(HelloWorld.java:29)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 java.lang.reflect.Method.invoke(Method.java:597)
 javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
 javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
 com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
 com.sun.el.parser.AstValue.getValue(AstValue.java:116)
 com.sun.el.parser.AstValue.getValue(AstValue.java:163)
 com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
 com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
 javax.faces.component.UIOutput.getValue(UIOutput.java:168)
 com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:338)
 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
 javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620)
 javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
 javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
 com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
 com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
 com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
root cause
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [_persistence_setperson_vh] or [_persistence_getperson_vh] is not defined in the object [com.googlecode.pu1.domain.PersonDetail].
Internal Exception: java.lang.NoSuchMethodException: com.googlecode.pu1.domain.PersonDetail._persistence_getperson_vh()
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[person]
Descriptor: RelationalDescriptor(com.googlecode.pu1.domain.PersonDetail --> [DatabaseTable(PERSONDETAIL)])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException

 org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:478)
 org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:406)
 org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:671)
 org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:633)
 org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:230)
 org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:368)
 org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151)
 org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207)
 org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195)
 org.springframework.orm.jpa.JpaTransactionManager.createEntityManagerForTransaction(JpaTransactionManager.java:400)
 org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:321)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:336)
 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:102)
 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
 $Proxy22.findAll(Unknown Source)
 com.googlecode.sandcode.helloworld.HelloWorld.getName(HelloWorld.java:29)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 java.lang.reflect.Method.invoke(Method.java:597)
 javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
 javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
 com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
 com.sun.el.parser.AstValue.getValue(AstValue.java:116)
 com.sun.el.parser.AstValue.getValue(AstValue.java:163)
 com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
 com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
 javax.faces.component.UIOutput.getValue(UIOutput.java:168)
 com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:338)
 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
 javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620)
 javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
 javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
 com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
 com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
 com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.

Apache Tomcat/6.0.20


Although the Exception list is very long, it boils down to one thing:

The JPA entity classes should be statically woven first by EclipseLink.

During project build, EclipseLink must be configured to weave your JPA entity classes.

You can also configure EclipseLink static weaving using Maven:
<build>
 <plugins>
   <plugin>
     <artifactId>maven-antrun-plugin</artifactId>
     <executions>
       <execution>
         <phase>process-classes</phase>
         <configuration>
           <tasks>
             <java classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
                   classpathref="maven.runtime.classpath" fork="true">
               <arg line="-loglevel FINE -persistenceinfo src/main/resources target/classes target/classes"/>
             </java>
           </tasks>
         </configuration>
         <goals>
           <goal>run</goal>
         </goals>
       </execution>
     </executions>
   </plugin>
 </plugins>
</build>
In addition, edit your META/INF/persistence.xml and add this property:
<property name="eclipselink.weaving" value="static" />