OpenEJB with Tomcat and Hibernate and JPA
문제
I'm using OpenEJB 3.1.3 embedded in Tomcat 5.5.26 and I use hibernate 3.6 as a JPA provider.
Here's my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
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_1_0.xsd">
<persistence-unit name="manager1" transaction-type="JTA">
<!-- provider is optional if you work with only 1 JPA provider -->
<!--
<provider>org.hibernate.ejb.HibernatePersistence</provider>
-->
<jta-data-source>java:/DefaultDS</jta-data-source>
<!--
<properties>
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" />
</properties>
-->
</persistence-unit>
</persistence>
Here's my code:
@Stateless
public class MapSearchManager implements MapSearchLocal, MapSearchRemote {
@PersistenceContext
private EntityManager em;
...
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public int queryDataSelectionNumRecords(MapSearchParamBean paramBean) {
Criteria c = createCriteria(paramBean);
c.setProjection(Projections.rowCount());
List l = c.list();
...
}
...
}
When I run tomcat with OpenEJB and my app war archives in it, the deployment failed with exception stack trace:
2010-10-27 18:13:35,374 - ERROR - Unable to deploy collapsed ear in war /wma: Exception: Creating application failed: C:\apache-tomcat-5.5.26\webapps\wma: javax/persistence/spi/ProviderUtil
org.apache.openejb.OpenEJBException: Creating application failed: C:\apache-tomcat-5.5.26\webapps\wma: javax/persistence/spi/ProviderUtil
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:666)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:449)
at org.apache.openejb.tomcat.catalina.TomcatWebAppBuilder.start(TomcatWebAppBuilder.java:249)
at org.apache.openejb.tomcat.catalina.GlobalListenerSupport.lifecycleEvent(GlobalListenerSupport.java:58)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4148)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:926)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:889)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.newInstance0(Class.java:326)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.openejb.assembler.classic.PersistenceBuilder.createEntityManagerFactory(PersistenceBuilder.java:179)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:489)
... 27 more
Upon searching, I found the class javax/persistence/spi/ProviderUtil
is part of Java EE 6/JPA 2.0 spec, which is not supported by OpenEJB. But I dont think in my app I use any JPA 2.0. I specifically mention in persistence.xml of 1.0. I know hibernate 3.6 supports JPA 2.0. I don't know if hibernate's Criteria API is part of JPA 2.0 but that shouldn't cause the problem bcause I have specifically mentioned 1.0 in persistence.xml.
Thank you for your help.
Regards
Will
해결책
Upon searching, I found the class javax/persistence/spi/ProviderUtil is part of Java EE 6/JPA 2.0 spec
This is correct.
which is not supported by OpenEJB.
Not yet. This is tracked by OPENEJB-1236.
But I dont think in my app I use any JPA 2.0
Whether you use JPA 2.0 features or not, some changes in various interfaces require explicit support from containers.
I specifically mention in persistence.xml of 1.0.
This doesn't make any difference, the container is still trying to load a JPA 2.0 implementation.
I don't know if hibernate's Criteria API is part of JPA 2.0
JPA 2.0 has a new Criteria API but Hibernate's Criteria API is part of... Hibernate. From what I can see, you're using Hibernate API and my suggestion is thus to use its JPA 1.0 implementation - i.e. Hibernate EntityManager 3.4.0.GA and its dependencies, which provide this API. You don't need JPA 2.0 anyway.
There isn't much info on the net about hibernate-jpa versions matching. So do you mean the Hibernate EntityManager and Annotations projects are the implementations of JPA 1.0 specification, and Hibernate Core project contains the core hibernate (non-JPA impl) and JPA 2.0 impl?
Hibernate EntityManager is the project that provides the JPA implementation.
- Hibernate EntityManager 3.4.0.GA is a JPA 1.0 implementation
- Hiberante EnittyManager 3.5+ is a JPA 2.0 implementation
It relies on Hibernate Core (and some other libraries). To use Hibernate EntityManager 3.4.0.GA, you'll need the following:
org.hibernate:hibernate-entitymanager:jar:3.4.0.GA:compile +- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile +- org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile +- org.hibernate:hibernate-core:jar:3.3.0.SP1:compile | +- antlr:antlr:jar:2.7.6:compile | \- commons-collections:commons-collections:jar:3.1:compile +- org.slf4j:slf4j-api:jar:1.5.10:compile +- dom4j:dom4j:jar:1.6.1:compile | \- xml-apis:xml-apis:jar:1.0.b2:compile +- javax.transaction:jta:jar:1.1:compile \- javassist:javassist:jar:3.4.GA:compile
I'm using Maven so I just have to declare a dependency on hibernate-entitymanager but if you don't, get a bundle from SourceForge.