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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:39:50  来源:igfitidea点击:

How to run Spring 3.0 PetClinic in tomcat with Hibernate backed JPA

javahibernatespringjpatomcat6

提问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:

我做了什么:

  1. Downloaded Tomcat 6.0.26 & Spring 3.0.1
  2. Downloaded PetClinic from https://src.springframework.org/svn/spring-samples/petclinic
  3. Built & deployed petclinic.war. Ran fine with default JDBC persistence.
  4. Edited webapps/WEB-INF/spring/applicationContext-jpa.xml and set jpaVendorAdaptorto Hibernate.
  5. Edited webapps/WEB-INF/web.xml and changed context-paramfrom applicationContext-jdbc.xml to applicationContext-jpa.xml
  6. Copied everything in the Spring 3.0.1 distribution to TOMCAT_HOME/lib.
  7. 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

  8. Uncommented line <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>in webapps/META-INF/context.xml.

  9. Same error. Added that line to TOMCAT_HOME/context.xml
  10. 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!

  1. 下载 Tomcat 6.0.26 & Spring 3.0.1
  2. https://src.springframework.org/svn/spring-samples/petclinic下载 PetClinic
  3. 构建和部署 petclinic.war。使用默认的 JDBC 持久性运行良好。
  4. 编辑 webapps/WEB-INF/spring/applicationContext-jpa.xml 并设置jpaVendorAdaptor为 Hibernate。
  5. 编辑 webapps/WEB-INF/web.xml 并context-param从 applicationContext-jdbc.xml更改为 applicationContext-jpa.xml
  6. 将 Spring 3.0.1 发行版中的所有内容复制到 TOMCAT_HOME/lib。
  7. 推出了tomcat。锯

    引起:java.lang.IllegalStateException:ClassLoader [org.apache.catalina.loader.WebappClassLoader] 不提供“addTransformer(ClassFileTransformer)”方法。指定自定义 LoadTimeWeaver 或使用 Spring 的代理启动 Java 虚拟机:-javaagent:spring-agent.jar

  8. <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>webapps/META-INF/context.xml 中未注释的行。

  9. 同样的错误。将该行添加到 TOMCAT_HOME/context.xml
  10. 部署没有错误。但是,当我做某事时它会发出一个错误说

    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:

好的,这是我所做的:

  1. Get Tomcat 6.0.26
  2. Checkout the petclinic sample:

    svn co https://src.springframework.org/svn/spring-samples/petclinic/trunk/ petclinic
    
  3. cdinto the petclinic directory

  4. Modify src/main/webapp/WEB-INF/spring/applicationContext-jpa.xmlto use Hibernate:
  5. Modify the src/main/webapp/WEB-INF/web.xmlto use the applicationContext-jpa.xml
  6. 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>
    
  7. Build the war

    mvn install
    
  8. Deploy it to Tomcat

    cp target/petclinic.war $TOMCAT_HOME/webapps
    
  9. Browse

    http://localhost:8080/petclinic
    
  1. 获取 Tomcat 6.0.26
  2. 查看宠物诊所样本:

    svn co https://src.springframework.org/svn/spring-samples/petclinic/trunk/ petclinic
    
  3. cd进入 petclinic 目录

  4. 修改src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml为使用Hibernate:
  5. 修改src/main/webapp/WEB-INF/web.xml使用applicationContext-jpa.xml
  6. 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>
    
  7. 建立War

    mvn install
    
  8. 部署到Tomcat

    cp target/petclinic.war $TOMCAT_HOME/webapps
    
  9. 浏览

    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