使用Tomcat和gradle进行Hibernate [英] Hibernate with Tomcat and gradle

查看:142
本文介绍了使用Tomcat和gradle进行Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我是一名Java新手。
我正在尝试使用Tomcat和Gradle设置Hibernate。构建运行正常,但看起来 persistence.xml 文件未被读取

Disclaimer: I'm a Java newbie. I'm trying to setup Hibernate with Tomcat and Gradle. The build runs correctly but it looks like the persistence.xml file is not read

我的项目结构如下:

├── 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 class:

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

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

When I run ./gradlew tomcatRunWar --stacktrace, the server is running on http://localhost:8080/play but when I try to access the page, I get the following exception:

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 文件需要进入战争的 WEB-INF / classes / META-INF / 目录。实现此目的的最佳方法是将其放入 src / main / resources / META-INF / 。如果你也想让它在测试中运行(运行类目录而不是Jar或War),你还需要配置:

The persistence.xml file needs to go into the War's WEB-INF/classes/META-INF/ directory. The best way to accomplish this is to put it into src/main/resources/META-INF/. If you also want this to work in tests (which run off class directories rather than a Jar or War), you additionally need to configure:

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

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

Above configuration is required because JPA expects to find the persistence.xml file and the corresponding entity classes in the same class directory or archive, but Gradle defaults to using different output directories for classes and resources.

PS:我假设 main 目录位于 src ,而不是 src 的兄弟,如第一张图所示。

PS: I assume the main directory is under src, not a sibling of src as shown in your first diagram.

这篇关于使用Tomcat和gradle进行Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆