免责声明:我是Java新手。我正在尝试使用Tomcat和Gradle设置Hibernate。生成正常运行,但看起来persistence.xml文件未读
persistence.xml
我的项目结构如下:
├── build.gradle └── src └── main ├── java │ └── com │ └── test │ ├── domain │ │ └── Person.java │ └── web │ └── EventManagerServlet.java └── webapp ├── META-INF │ └── web.xml └── WEB-INF └── classes └── persistence.xml
build.gradle文件内容:
build.gradle
apply plugin: 'java' apply plugin: 'idea' apply plugin: 'tomcat' apply plugin: 'war' sourceCompatibility = 1.5 version = '1.0' repositories { mavenCentral() } buildscript { repositories { jcenter() } dependencies { classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.2.3' } } dependencies { compile 'javax.servlet:javax.servlet-api:3.0.1' compile group: 'org.hibernate', name: 'hibernate-core', version: '4.3.5.Final' compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.13' def tomcatVersion = '7.0.11' tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}" tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") { exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj' } } war { webXml = file('src/main/webapp/META-INF/web.xml') }
本EventManagerServlet类:
EventManagerServlet
package com.test.web; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; public class EventManagerServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com"); EntityManager entityManager = entityManagerFactory.createEntityManager(); } }
该persistence.xml文件中:
<persistence 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_2_0.xsd" version="2.0"> <persistence-unit name="com" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.test.domain.Person</class> <properties> <property name="hibernate.archive.autodetection" value="class, hbm"/> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.default_schema">hibernate</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto" value="create"/> <property name="hibernate.c3p0.min_size" value="5"/> <property name="hibernate.c3p0.max_size" value="20"/> <property name="hibernate.c3p0.timeout" value="300"/> <property name="hibernate.c3p0.max_statements" value="50"/> <property name="hibernate.c3p0.idle_test_period" value="3000"/> </properties> </persistence-unit> </persistence>
当我运行时./gradlew tomcatRunWar --stacktrace,服务器正在运行,http://localhost:8080/play但是当我尝试访问该页面时,出现以下异常:
./gradlew tomcatRunWar --stacktrace
http://localhost:8080/play
Servlet.service() for servlet [Event Manager] in context with path [/play] threw exception javax.persistence.PersistenceException: No Persistence provider for EntityManager named com at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
战争目录结构:
├── META-INF │ ├── MANIFEST.MF │ └── web.xml └── WEB-INF ├── classes │ ├── com │ │ └── test │ │ ├── domain │ │ │ └── Person.class │ │ └── web │ │ └── EventManagerServlet.class │ └── persistence.xml ├── lib │ ├── antlr-2.7.7.jar │ ├── dom4j-1.6.1.jar │ ├── hibernate-commons-annotations-4.0.4.Final.jar │ ├── hibernate-core-4.3.5.Final.jar │ ├── hibernate-jpa-2.1-api-1.0.0.Final.jar │ ├── jandex-1.1.0.Final.jar │ ├── javassist-3.18.1-GA.jar │ ├── javax.servlet-api-3.0.1.jar │ ├── jboss-logging-3.1.3.GA.jar │ ├── jboss-logging-annotations-1.2.0.Beta1.jar │ ├── jboss-transaction-api_1.2_spec-1.0.0.Final.jar │ ├── mysql-connector-java-5.1.13.jar │ └── xml-apis-1.0.b2.jar └── web.xml
编辑
我必须添加 build.gradle
classpath 'org.hibernate:hibernate-entitymanager:4.1.7.Final'
该persistence.xml文件需要进入War的WEB-INF/classes/META- INF/目录。最好的方法是将其放入src/main/resources/META- INF/。如果您还希望它可以在测试中运行(从类目录而不是Jar或War运行),则还需要配置:
WEB-INF/classes/META- INF/
src/main/resources/META- INF/
sourceSets.all { output.resourcesDir = output.classesDir }
需要进行上述配置,因为JPA希望persistence.xml在同一类目录或存档中找到文件和相应的实体类,但是Gradle默认将类和资源使用不同的输出目录。
PS:我认为该main目录位于下src,而不是src您的第一个图表中所示的同级目录。
main
src