java 如何使用 Hibernate 支持的 JPA 在 tomcat 中运行 Spring 3.0 PetClinic
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2536292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run Spring 3.0 PetClinic in tomcat with Hibernate backed JPA
提问by Enno Shioji
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.
编辑:有关正确的程序,请参阅 Pascal 的回答。
My wrong (since I did not disabled LoadTimeWeaving) procedure is left for reference..:
我的错误(因为我没有禁用 LoadTimeWeaving)程序留作参考..:
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
jpaVendorAdaptorto Hibernate. - Edited webapps/WEB-INF/web.xml and changed
context-paramfrom 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!
- 下载 Tomcat 6.0.26 & Spring 3.0.1
- 从https://src.springframework.org/svn/spring-samples/petclinic下载 PetClinic
- 构建和部署 petclinic.war。使用默认的 JDBC 持久性运行良好。
- 编辑 webapps/WEB-INF/spring/applicationContext-jpa.xml 并设置
jpaVendorAdaptor为 Hibernate。 - 编辑 webapps/WEB-INF/web.xml 并
context-param从 applicationContext-jdbc.xml更改为 applicationContext-jpa.xml - 将 Spring 3.0.1 发行版中的所有内容复制到 TOMCAT_HOME/lib。
推出了tomcat。锯
引起:java.lang.IllegalStateException:ClassLoader [org.apache.catalina.loader.WebappClassLoader] 不提供“addTransformer(ClassFileTransformer)”方法。指定自定义 LoadTimeWeaver 或使用 Spring 的代理启动 Java 虚拟机:-javaagent:spring-agent.jar
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>webapps/META-INF/context.xml 中未注释的行。- 同样的错误。将该行添加到 TOMCAT_HOME/context.xml
部署没有错误。但是,当我做某事时它会发出一个错误说
java.lang.NoClassDefFoundError: javax/transaction/SystemException at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:39)11.按照 scaffman 的建议,将 javax.transaction 的范围从测试更改为默认值(只是删除了测试)。
12.运行正常!!谢谢!
采纳答案by Pascal Thivent
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/ petcliniccdinto the petclinic directory- Modify
src/main/webapp/WEB-INF/spring/applicationContext-jpa.xmlto use Hibernate:- COMMENT the
<context:load-time-weaver>(load-time weaver SHOULD NOTbe 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.xmlto use theapplicationContext-jpa.xml Modify the
pom.xmlto bundle jta.jarin 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 installDeploy it to Tomcat
cp target/petclinic.war $TOMCAT_HOME/webappsBrowse
http://localhost:8080/petclinic
- 获取 Tomcat 6.0.26
查看宠物诊所样本:
svn co https://src.springframework.org/svn/spring-samples/petclinic/trunk/ petcliniccd进入 petclinic 目录- 修改
src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml为使用Hibernate:- 评论
<context:load-time-weaver>(加载时编织器不应与 Hibernate 一起使用,这是用于 Toplink) - 声明 Hibernate 为
jpaVendorAdapter(注释 Toplink 部分,取消注释 Hibernate 部分)
- 评论
- 修改
src/main/webapp/WEB-INF/web.xml使用applicationContext-jpa.xml 在war中修改
pom.xmlto bundle jta.jar(如@skaffman 所指出的):<dependency> <groupId>javax.transaction</groupId> <artifactId>com.springsource.javax.transaction</artifactId> <version>1.1.0</version> <!--scope>test</scope--> </dependency>建立War
mvn install部署到Tomcat
cp target/petclinic.war $TOMCAT_HOME/webapps浏览
http://localhost:8080/petclinic
回答by skaffman
Looks like a problem with PetClinic packaging:
看起来像 PetClinic 包装的问题:
http://forum.springsource.org/showthread.php?t=85042
http://forum.springsource.org/showthread.php?t=85042
and
和
http://jira.springframework.org/browse/SPR-6880
http://jira.springframework.org/browse/SPR-6880
There's fix in there for the pom.xml
那里有修复 pom.xml

