PersistenceException:找不到持久性单元名为default的模式生成的持久性提供程序 [英] PersistenceException: No persistence provider found for schema generation for persistence-unit named default

查看:730
本文介绍了PersistenceException:找不到持久性单元名为default的模式生成的持久性提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置一个运行java主类的gradle任务,该主类用于生成SQL模式。



我没有persistence.xml配置文件。

以下是我的配置和代码:

我的gradle任务:

 任务JpaSchemaExport(类型:JavaExec){
描述导出Jpa模式
dependsOn compileJava
main =com.bignibou .tools.jpa.JpaSchemaExport
classpath = sourceSets.main.runtimeClasspath + configurations.compile
}

我的导出实用程序:

  public class JpaSchemaExport {

public static void main (String [] args)抛出IOException {
// execute(args [0],args [1]);
execute(default,build / schema.sql);
System.exit(0);


public static void execute(String persistenceUnitName,String destination){
final属性persistenceProperties = new Properties();

// XXX强制持久性属性:删除数据库目标
persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO,);
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION,none);

// XXX强制持久性属性:定义从元数据到目的地的创建脚本目标
// PersistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS,true);
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION,create);
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE,metadata);
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET,destination);

Persistence.generateSchema(persistenceUnitName,persistenceProperties);


$ / code $ / pre

我的数据配置:

  @Bean 
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setPackagesToScan(com.bignibou.domain);
emf.setDataSource(dataSource);
emf.setPersistenceProvider(new HibernatePersistenceProvider());
emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
emf.setJpaPropertyMap(propertiesMap());
返回emf;
}

私人地图< String,String> propertiesMap(){
Map< String,String> propertiesMap = new HashMap<>();
propertiesMap.put(hibernate.dialect,hibernateDialect);
propertiesMap.put(hibernate.hbm2ddl.auto,hibernateHbm2ddlAuto);
propertiesMap.put(hibernate.ejb.naming_strategy,hibernateEjbNamingStrategy);
propertiesMap.put(hibernate.connection.charSet,hibernateConnectionCharset);
propertiesMap.put(hibernate.show_sql,hibernateLogSqlInfo);
propertiesMap.put(hibernate.format_sql,hibernateLogSqlInfo);
propertiesMap.put(hibernate.use_sql_comments,hibernateLogSqlInfo);
propertiesMap.put(hibernate.generate_statistics,hibernateGenerateStatistics);
propertiesMap.put(hibernate.cache.use_second_level_cache,hibernateCacheUseSecondLevelCache);
propertiesMap.put(hibernate.cache.region.factory_class,org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory);
propertiesMap.put(javax.persistence.sharedCache.mode,ENABLE_SELECTIVE);
返回propertiesMap;
}

这是我得到的异常:

 线程main中的异常javax.persistence.PersistenceException:未找到持久性提供程序,用于持久性单元,名为default 
,位于javax.persistence.Persistence .generateSchema(Persistence.java:93)
at com.bignibou.tools.jpa.JpaSchemaExport.execute(JpaSchemaExport.java:31)
at com.bignibou.tools.jpa.JpaSchemaExport.main(JpaSchemaExport .java:14)

编辑:确实收到警告:

 :bignibou-server:JpaSchemaExport 
2015-05-16 14:46:44,423 [main] WARN org.hibernate .ejb.HibernatePersistence - HHH015016:遇到不推荐使用的javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence];改为使用[org.hibernate.jpa.HibernatePersistenceProvider]。
2015-05-16 14:46:44,423 [main] WARN org.hibernate.ejb.HibernatePersistence - HHH015016:遇到不推荐使用的javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence];改为使用[org.hibernate.jpa.HibernatePersistenceProvider]。
2015-05-16 14:46:44,423 [main] WARN org.hibernate.ejb.HibernatePersistence - HHH015016:遇到不推荐使用的javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence];改为使用[org.hibernate.jpa.HibernatePersistenceProvider]。
线程main中的异常javax.persistence.PersistenceException:未找到持久性提供程序,用于名为default的持久性单元的模式生成
位于javax.persistence.Persistence.generateSchema(Persistence.java:93)
at com.bignibou.tools.jpa.JpaSchemaExport.execute(JpaSchemaExport.java:32)
at com.bignibou.tools.jpa.JpaSchemaExport.main(JpaSchemaExport.java:14)
:bignibou -server:JpaSchemaExport FAILED


解决方案

> JpaSchemaExport 不会在Spring Context中执行,只要我可以从您的代码中看到。



当您运行 Gradle或命令行中的JpaSchemaExport 创建自己的 EntityManager ,它与Spring无关并且寻找持久性.xml 文件放入 META-INF 目录中。对于Spring Boot应用程序,这个文件是不需要的,因此可能不存在。



当我运行一些类似JpaSchemaExport
的输出时, p>

 
[main] INFO ohjbiPersistenceXmlParser - HHH000318:在类路径中找不到任何META-INF / persistence.xml文件
[主] DEBUG ohjpa.HibernatePersistenceProvider - 定位和解析0个持久性单元;检查每个
[main] DEBUG ohjpa.HibernatePersistenceProvider - 发现没有匹配的持久性单元线程main中的异常
javax.persistence.PersistenceException:未找到持久性提供程序
用于持久化架构生成 - 在
处名为default的单元
处的javax.persistence.Persistence.generateSchema(Persistence.java:93)com.example.springboot.jpa.JpaSchemaExport.execute(JpaSchemaExport.java:42)
at
com.example.springboot.jpa.JpaSchemaExport.main(JpaSchemaExport.java:14)

Spring Boot命令行应用程序(有一个Spring Context)如下所示:

  @SpringBootApplication 
public class Application实现CommandLineRunner {


public static void main(String [] args){
SpringApplication.run(Application.class);
}

@Override
public void run(String ... strings)throws Exception {

}

}

即您的 JpaSchemaExport 可能被重写为

  @SpringBootApplication 
public class JpaSchemaExport implements CommandLineRunner {


public static void main(String [] args){
//也许激活一个特殊的spring配置文件来启用
//hibernate .hbm2ddl.auto,验证|更新|创建| create-drop
// AvailableSettings.SCHEMA_GEN_DATABASE_ACTION,none
// AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS,true
// AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION,create
// AvailableSettings。 SCHEMA_GEN_CREATE_SOURCE,metadata
// AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET,destination

SpringApplication.run(JpaSchemaExport.class);
}

@Override
public void run(String ... strings)throws Exception {
//此处不需要
}

}

作为替代方案,您可以创建 META-INF /persistence.xml 它可以被Spring Boot应用程序以及 JpaSchemaExport 使用。


I have tried to set up a gradle task that runs a java main class that is intended to generate a SQL schema.

I have no persistence.xml configuration file.

Here is my configuration and code:

My gradle task:

task JpaSchemaExport(type: JavaExec){
       description "Exports Jpa schema"
       dependsOn compileJava
       main = "com.bignibou.tools.jpa.JpaSchemaExport"
       classpath = sourceSets.main.runtimeClasspath + configurations.compile
    }

My export utility:

public class JpaSchemaExport {

    public static void main(String[] args) throws IOException {
        // execute(args[0], args[1]);
        execute("default", "build/schema.sql");
        System.exit(0);
    }

    public static void execute(String persistenceUnitName, String destination) {
        final Properties persistenceProperties = new Properties();

        // XXX force persistence properties : remove database target
        persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, "");
        persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none");

        // XXX force persistence properties : define create script target from metadata to destination
        // persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS, "true");
        persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create");
        persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata");
        persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination);

        Persistence.generateSchema(persistenceUnitName, persistenceProperties);
    }
}

My data configuration:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setPackagesToScan("com.bignibou.domain");
    emf.setDataSource(dataSource);
    emf.setPersistenceProvider(new HibernatePersistenceProvider());
    emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    emf.setJpaPropertyMap(propertiesMap());
    return emf;
}

private Map<String, String> propertiesMap() {
    Map<String, String> propertiesMap = new HashMap<>();
    propertiesMap.put("hibernate.dialect", hibernateDialect);
    propertiesMap.put("hibernate.hbm2ddl.auto", hibernateHbm2ddlAuto);
    propertiesMap.put("hibernate.ejb.naming_strategy", hibernateEjbNamingStrategy);
    propertiesMap.put("hibernate.connection.charSet", hibernateConnectionCharset);
    propertiesMap.put("hibernate.show_sql", hibernateLogSqlInfo);
    propertiesMap.put("hibernate.format_sql", hibernateLogSqlInfo);
    propertiesMap.put("hibernate.use_sql_comments", hibernateLogSqlInfo);
    propertiesMap.put("hibernate.generate_statistics", hibernateGenerateStatistics);
    propertiesMap.put("hibernate.cache.use_second_level_cache", hibernateCacheUseSecondLevelCache);
    propertiesMap.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
    propertiesMap.put("javax.persistence.sharedCache.mode", "ENABLE_SELECTIVE");
    return propertiesMap;
}

Here is the exception I get:

Exception in thread "main" javax.persistence.PersistenceException: No persistence provider found for schema generation for persistence-unit named default
 at javax.persistence.Persistence.generateSchema(Persistence.java:93)
 at com.bignibou.tools.jpa.JpaSchemaExport.execute(JpaSchemaExport.java:31)
 at com.bignibou.tools.jpa.JpaSchemaExport.main(JpaSchemaExport.java:14)

edit: I do get warnings indeed:

:bignibou-server:JpaSchemaExport
2015-05-16 14:46:44,423 [main] WARN  org.hibernate.ejb.HibernatePersistence - HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
2015-05-16 14:46:44,423 [main] WARN  org.hibernate.ejb.HibernatePersistence - HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
2015-05-16 14:46:44,423 [main] WARN  org.hibernate.ejb.HibernatePersistence - HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Exception in thread "main" javax.persistence.PersistenceException: No persistence provider found for schema generation for persistence-unit named default
 at javax.persistence.Persistence.generateSchema(Persistence.java:93)
 at com.bignibou.tools.jpa.JpaSchemaExport.execute(JpaSchemaExport.java:32)
 at com.bignibou.tools.jpa.JpaSchemaExport.main(JpaSchemaExport.java:14)
:bignibou-server:JpaSchemaExport FAILED 

解决方案

Your JpaSchemaExport is not executed within a Spring Context as far as I can see from your code.

When you run your JpaSchemaExport from Gradle or command line you create your own EntityManager which has nothing to do with Spring and looks for a persistence.xml file in the META-INF directory. For Spring Boot Applications this file is not needed and thus may not exist.

When I run something which looks similar your JpaSchemaExport the output is something like

[main] INFO  o.h.j.b.i.PersistenceXmlParser - HHH000318: Could not find any META-INF/persistence.xml file in the class path
[main] DEBUG o.h.jpa.HibernatePersistenceProvider - Located and parsed 0  persistence units; checking each 
[main] DEBUG o.h.jpa.HibernatePersistenceProvider - Found no matching persistence units Exception in thread "main"
javax.persistence.PersistenceException: No persistence provider found
for schema generation for persistence-unit named default    at
javax.persistence.Persistence.generateSchema(Persistence.java:93)   at
com.example.springboot.jpa.JpaSchemaExport.execute(JpaSchemaExport.java:42)
    at
com.example.springboot.jpa.JpaSchemaExport.main(JpaSchemaExport.java:14)

A Spring Boot Commandline Application (one that has a Spring Context) looks something like:

@SpringBootApplication
public class Application implements CommandLineRunner {


    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Override
    public void run(String... strings) throws Exception {

    }

}

i.e. your JpaSchemaExport may be rewritten as

@SpringBootApplication
public class JpaSchemaExport implements CommandLineRunner {


    public static void main(String[] args) {
        // maybe activate a special spring profile to enable 
        // "hibernate.hbm2ddl.auto", validate | update | create | create-drop
        // AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none" 
        // AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS, "true"
        // AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create"
        // AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata"
        // AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination

        SpringApplication.run(JpaSchemaExport.class);
    }

    @Override
    public void run(String... strings) throws Exception {
       // nothing needed here 
    }

}

As an alternative you can create a META-INF/persistence.xml which can be used by your Spring Boot application as well as by your JpaSchemaExport.

这篇关于PersistenceException:找不到持久性单元名为default的模式生成的持久性提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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