gpt4 book ai didi

java - 使用 Tomcat 和 gradle hibernate

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:14 26 4
gpt4 key购买 nike

免责声明:我是 Java 新手。我正在尝试使用 Tomcat 和 Gradle 设置 Hibernate。构建运行正确,但看起来 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 文件的内容:

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 类:

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 上运行,但是当我尝试访问该页面时,我得到以下异常:

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)

war目录结构:

├── 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),您还需要配置:

sourceSets.all {
output.resourcesDir = output.classesDir
}

以上配置是必需的,因为 JPA 希望在同一个类目录或归档中找到 persistence.xml 文件和相应的实体类,但 Gradle 默认为类和资源使用不同的输出目录。

PS:我假设 main 目录在 src 下,而不是第一个图中所示的 src 的同级目录。

关于java - 使用 Tomcat 和 gradle hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22972947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com