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
My project structure is as follow:
├── build.gradle
└── src
└── main
├── java
│ └── com
│ └── test
│ ├── domain
│ │ └── Person.java
│ └── web
│ └── EventManagerServlet.java
└── webapp
├── META-INF
│ └── web.xml
└── WEB-INF
└── classes
└── persistence.xml
The content of the build.gradle file:
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')
}
The 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();
}
}
The 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>
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)
The war directory structure:
├── 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
EDIT
I had to add in the build.gradle
classpath 'org.hibernate:hibernate-entitymanager:4.1.7.Final'