如何使用Hibernate支持的JPA在tomcat中运行Spring 3.0 PetClinic [英] How to run Spring 3.0 PetClinic in tomcat with Hibernate backed JPA
问题描述
编辑:正确的程序请参阅帕斯卡的回答。
我的错误的(因为我没有禁用LoadTimeWeaving)过程留作参考..:
我做了什么:
- 已下载的Tomcat 6.0.26& Spring 3.0.1
- 从 https:/下载PetClinic /src.springframework.org/svn/spring-samples/petclinic
- 建造&部署petclinic.war。使用默认的JDBC持久性保持良好。
- 将Hibernate 。
- 编辑webapps / WEB-INF / web.xml并将applicationContext-jdbc.xml中的
context-param
更改为applicationContext-jpa .xml - 将Spring 3.0.1发行版中的所有内容复制到TOMCAT_HOME / lib中。
-
推出tomcat。看到
由于:java.lang.IllegalStateException:ClassLoader [org.apache.catalina.loader.WebappClassLoader]没有提供'addTransformer(ClassFileTransformer)'方法。使用Spring的代理指定自定义的LoadTimeWeaver或启动Java虚拟机:-javaagent:spring-agent.jar
-
未注释的行
< Loader loaderClass =org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader/>
在webapps / META-INF / context.xml中。 - 同样的错误。将该行添加到TOMCAT_HOME / context.xml中
部署时没有错误。但是,当我执行某些操作时,它会发出一个错误消息,指出在org.hibernate.ejb上的javax / transaction / SystemException异常:
java.lang.NoClassDefFoundError .EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:39)
11.将javax.transaction的范围从测试更改为默认值(仅删除测试) ,如脚手所示。
12.跑得好!谢谢! p> - 获取Tomcat 6.0.26
签出petclinic样本:
svn co https://src.springframework.org/svn/spring-samples/petclinic/trunk/ petclinic
-
cd
进入petclinic目录 - 修改
src / main / webapp / WEB-INF / spring / applicationContext-jpa.xml
使用Hibernate:
- 评论
< context:load-time-weaver>
(加载时间织机 SHOULD NOT 与Hibernate ,这是Toplink的) - 声明Hibernate为
jpaVendorAdapter
(注释Toplink部分,取消注释Hibernate部分)
- 评论
- 修改
src / main /webapp/WEB-INF/web.xml
来使用applicationContext-jpa.xml
-
在战争中修改
pom.xml
以捆绑 jta.jar (正如@skaffman指出的那样):< dependency>
< groupId> javax.transaction< / groupId>
< artifactId> com.springsource.javax.transaction< / artifactId>
< version> 1.1.0< / version>
<! - scope> test< / scope - >
< /依赖关系>
-
建立战争
mvn install
-
将其部署到Tomcat
cp target / petclinic.war $ TOMCAT_HOME / webapps
-
浏览
http:// localhost:8080 / petclinic
OK, this probably is supposed to be the easiest thing in the world, but I've been trying for the entire day, and it's still not working.. Any help is highly appreciated!
EDIT: For the correct procedure, please see Pascal's answer.
My wrong (since I did not disabled LoadTimeWeaving) procedure is left for reference..:
What I did:
- Downloaded Tomcat 6.0.26 & Spring 3.0.1
- Downloaded PetClinic from https://src.springframework.org/svn/spring-samples/petclinic
- Built & deployed petclinic.war. Ran fine with default JDBC persistence.
- Edited webapps/WEB-INF/spring/applicationContext-jpa.xml and set
jpaVendorAdaptor
to Hibernate. - Edited webapps/WEB-INF/web.xml and changed
context-param
from applicationContext-jdbc.xml to applicationContext-jpa.xml - Copied everything in the Spring 3.0.1 distribution to TOMCAT_HOME/lib.
Launched tomcat. Saw
Caused by: java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-agent.jar
Uncommented line
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
in webapps/META-INF/context.xml.- Same error. Added that line to TOMCAT_HOME/context.xml
Deployed without error. However, when I do something it will issue an error saying
java.lang.NoClassDefFoundError: javax/transaction/SystemException at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:39)
11.Changed scope of javax.transaction from test to default (just deleted test), as suggested by scaffman.
12.Runs fine!! Thank you!
Ok, here is what I did:
- Get Tomcat 6.0.26
Checkout the petclinic sample:
svn co https://src.springframework.org/svn/spring-samples/petclinic/trunk/ petclinic
cd
into the petclinic directory- Modify
src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml
to use Hibernate:- COMMENT the
<context:load-time-weaver>
(load-time weaver SHOULD NOT be used with Hibernate, this is for Toplink) - Declare Hibernate as
jpaVendorAdapter
(comment the Toplink part, uncomment the Hibernate part)
- COMMENT the
- Modify the
src/main/webapp/WEB-INF/web.xml
to use theapplicationContext-jpa.xml
Modify the
pom.xml
to bundle jta.jar in the war (as pointed out by @skaffman):<dependency> <groupId>javax.transaction</groupId> <artifactId>com.springsource.javax.transaction</artifactId> <version>1.1.0</version> <!--scope>test</scope--> </dependency>
Build the war
mvn install
Deploy it to Tomcat
cp target/petclinic.war $TOMCAT_HOME/webapps
Browse
http://localhost:8080/petclinic
这篇关于如何使用Hibernate支持的JPA在tomcat中运行Spring 3.0 PetClinic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!