hibernate - 持久性异常 : No persistence provider found for schema generation for persistence-unit named default

标签 hibernate jpa gradle hibernate-tools

我试图设置一个运行 java 主类的 gradle 任务,该类旨在生成 SQL 模式。

我没有persistence.xml 配置文件。

这是我的配置和代码:

我的毕业任务:

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

我的导出实用程序:
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);
    }
}

我的数据配置:
@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;
}

这是我得到的异常(exception):
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)

编辑 :我确实收到警告:
: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 

最佳答案

您的 JpaSchemaExport就我从您的代码中看到的而言,它不在 Spring 上下文中执行。

当您运行 JpaSchemaExport 时从 Gradle 或命令行创建您自己的 EntityManager与 Spring 无关,查找 persistence.xml META-INF 中的文件目录。对于 Spring Boot 应用程序,不需要此文件,因此可能不存在。

当我运行看起来与您的 JpaSchemaExport 相似的东西时
输出是这样的

[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 {

    }

}

即您的 JpaSchemaExport可以改写为
@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 
    }

}

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

关于hibernate - 持久性异常 : No persistence provider found for schema generation for persistence-unit named default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30225183/

相关文章:

java - Hibernate:哪一列???将数据类型 nvarchar 转换为数字时出错

java - org.hibernate.exception.JDBCConnectionException : Error calling Driver#connect

java - JPA 一对多/多对多示例

hibernate - 在 JPQL 查询中应用新的 map() 函数来检索包含列详细信息的结果

java - Gradle忽略守护程序忽略标志

java - Hibernate 忽略 'lazy' 获取类型并立即加载属性

java 如何设置自增属性

java - Hibernate Inheritance.TABLE_PER_CLASS 策略

jvm - Gradle 应用程序插件 : How can I run jvm application with -javaagent options?

gradle - 从构建脚本配置Gradle插件