找不到persistence.xml - 带有NoSql数据库的Gradle-Spring-MVC项目

时间:2016-11-03 10:22:58

标签: java spring hibernate intellij-idea gradle

我正在使用Intellij 2016和Gradle创建一个注释驱动的Spring MVC应用程序。我的项目结构如下所示:

enter image description here

我故意将persistence.xml复制到文件夹src/main/java/META-INF/persistence.xmlsrc/main/resources/META-INF/persistence.xml中,但在执行Gradle bootRun任务时,我收到以下错误:

11:18:40: Executing external task 'bootRun'...
:compileJava
:processResources
:classes
:findMainClass
:bootRun
11:18:52.960 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named ogm-jpa-tutorial
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at my.app.main.Application.main(Application.java:27)
:bootRun FAILED

似乎bootRun任务无法从类路径中获取persistence.xml

是否有人知道该错误背后有什么问题并有解决方法?

编辑javaguy,缺少EntityManager的初始化:

/*
Content of the class my.app.main.Application
*/
package my.app.main;

import my.app.controllers.GreetingController;
import my.app.model.Greeting;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.TransactionManager;

@SpringBootApplication
@ComponentScan({"my.app.*"})
public class Application {

    private static final Log logger = LoggerFactory.make();

    public static void main(String[] args) {

        TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ogm-jpa-tutorial");

        SpringApplication.run(Application.class, args);
    }

}

编辑Asterisk Ninja失踪persistence.xml

<?xml version="1.0"?>
<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="ogm-jpa-tutorial" transaction-type="JTA">
        <!-- Use the Hibernate OGM provider: configuration will be transparent -->
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <properties>
            <!-- Here you will pick which NoSQL technology to use, and configure it;
                 in this example we start a local in-memory Infinispan node. -->
            <property name="hibernate.ogm.datastore.provider" value="infinispan"/>
        </properties>
    </persistence-unit>
</persistence>

编辑,遗失build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-serving-web-content'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-devtools")
    compile("org.hibernate.ogm:hibernate-ogm-bom:5.0.2.Final")
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
    compile group: 'javax.transaction', name: 'jta', version: '1.1'

    compile group: 'org.jboss.narayana.jta', name: 'narayana-jta', version: '5.3.5.Final'
    compile group: 'org.hibernate', name: 'hibernate-annotations', version: '3.5.6-Final'

    testCompile("junit:junit")
}

1 个答案:

答案 0 :(得分:1)

我通过以下方式调整build.gradle文件解决了我的问题:

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-serving-web-content'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-devtools")
    compile("org.hibernate.ogm:hibernate-ogm-bom:5.0.2.Final")
    compile group: 'org.hibernate.ogm', name: 'hibernate-ogm-infinispan', version: '5.0.2.Final'
    compile group: 'org.jboss.spec.javax.transaction', name: 'jboss-transaction-api_1.2_spec', version: '1.0.0.Final'
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
    compile group: 'org.jboss.narayana.jta', name: 'narayana-jta', version: '5.3.5.Final'
    compile group: 'org.jboss', name: 'jboss-transaction-spi', version: '7.5.0.Final'
    compile group: 'javax.transaction', name: 'jta', version: '1.1'
    compile group: 'org.hibernate.common', name: 'hibernate-commons-annotations', version: '5.0.1.Final'

    testCompile("junit:junit")
}

评论:

  • 似乎persistence.xml被正确拾取,但是 EntitiyManager无法正确初始化<provider>HibernateOgmPersistence

因此进行了以下更改:

  • 为特定的EntitiyManager添加了一些依赖项 MongoDB内存中实例