我在设置Hibernate以使用Tomcat时遇到了一些问题。
这是项目结构:
├── build.gradle
└── src
└── main
├── java
│ └── com
│ └── test
│ ├── domain
│ │ └── Person.java
│ └── web
│ └── Servlet.java
├── resources
│ └── META-INF
│ └── persistence.xml
└── webapp
└── META-INF
└── web.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 'org.hibernate:hibernate-entitymanager:4.3.4.Final'
compile group: 'org.hibernate', name: 'hibernate-core', version: '4.3.4.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.30'
def tomcatVersion = '8.0.5'
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')
}
Servlet
类:
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 Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com");
EntityManager entityManager = entityManagerFactory.createEntityManager();
}
}
WAR结构:
├── META-INF
│ ├── MANIFEST.MF
│ └── web.xml
└── WEB-INF
├── classes
│ ├── META-INF
│ │ └── persistence.xml
│ └── com
│ └── test
│ ├── domain
│ │ └── Person.class
│ └── web
│ └── Servlet.class
├── lib
│ ├── antlr-2.7.7.jar
│ ├── dom4j-1.6.1.jar
│ ├── hibernate-commons-annotations-4.0.4.Final.jar
│ ├── hibernate-core-4.3.4.Final.jar
│ ├── hibernate-entitymanager-4.3.4.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.30.jar
│ └── xml-apis-1.0.b2.jar
└── web.xml
当我使用gradle构建应用程序时,一切正常,但当我尝试访问该页面时,我得到:
java.lang.IllegalArgumentException: File [/Users/david/IdeaProjects/play/build/libs/play-1.0.war!/WEB-INF/classes] referenced by given URL [file:/Users/david/IdeaProjects/play/build/libs/play-1.0.war!/WEB-INF/classes] does not exist
org.hibernate.jpa.boot.archive.internal.StandardArchiveDescriptorFactory.buildArchiveDescriptor(StandardArchiveDescriptorFactory.java:73)
org.hibernate.jpa.boot.archive.internal.StandardArchiveDescriptorFactory.buildArchiveDescriptor(StandardArchiveDescriptorFactory.java:48)
org.hibernate.jpa.boot.scan.spi.AbstractScannerImpl.buildArchiveDescriptor(AbstractScannerImpl.java:95)
org.hibernate.jpa.boot.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:70)
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.scan(EntityManagerFactoryBuilderImpl.java:723)
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:219)
org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:51)
org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:129)
org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:93)
org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:86)
org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:101)
org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:67)
org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
com.test.web.Servlet.doGet(Servlet.java:13)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
我完全不知道它为什么要加载这个文件。