PersistenceException: constant provider not found to generate schema for default save unit

I tried to configure the gradle task, which runs the main java class, which is designed to create an SQL schema.

I do not have 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 really get warnings:

 :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 
+3
hibernate jpa hibernate-tools gradle
source share
5 answers

Your JpaSchemaExport not running in the Spring context, as far as I can see from your code.

When you start JpaSchemaExport from Gradle or the command line, you create your own EntityManager that has nothing to do with Spring and looks for the persistence.xml file in the META-INF . For Spring boot applications, this file is not needed and therefore may not exist.

When I run something similar to your JpaSchemaExport, the output is similar to

 [main] INFO ohjbiPersistenceXmlParser - HHH000318: Could not find any META-INF / persistence.xml file in the class path
 [main] DEBUG ohjpa.HibernatePersistenceProvider - Located and parsed 0 persistence units;  checking each 
 [main] DEBUG ohjpa.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 A boot command line application (which has a Spring context) looks something like this:

 @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 can 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 } } 

Alternatively, you can create META-INF/persistence.xml that your Spring application can use to load, as well as your JpaSchemaExport .

+3
source share

Your code is very nice and clean. Are there other warnings when starting the server, for example, the log4j properties are missing. Etc.,

I think the problem is with some missing jar or classpath elements, although not sure about that.

+2
source share

It seems that Persistence.generateSchema() uses the PersistenceProviderResolverHolder , which contains the PersistenceProviderResolver , to get a list of all available PersistenceProvider s. You might need to try implementing your own PersistenceProviderResolver (total of 2 methods) to return the one you want to use ( org.hibernate.jpa.HibernatePersistenceProvider ) before calling Persistence.generateSchema() .

 PersistenceProviderResolverHolder.setPersistenceProviderResolver(new PersistenceProviderResolver() { public List<PersistenceProvider> getPersistenceProviders() { return Collections.singletonList(new HibernatePersistenceProvider()); } public void clearCachedProviders() {} }); 
+2
source share

You will need to set an explicit name for the save module, which is created programmatically using the following method, and then be sure to call the afterPropertiesSet () method:

 emf.setPersistenceUnitName( "puname" ); ... emf.afterPropertiesSet(); 

Refer to your persistence device using this name instead of "default"

+1
source share

Creating a schema with Spring Boot application:

 import org.hibernate.jpa.AvailableSettings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import java.util.Properties; @SpringBootApplication public class JpaSchemaExport implements CommandLineRunner { @Autowired LocalContainerEntityManagerFactoryBean entityManagerFactory; public static void main(String... args) { new SpringApplicationBuilder(JpaSchemaExport.class).run(args); } @Override public void run(String... args) throws Exception { 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, "./generated-schema.ddl"); // generate sql with semicolon - workaround for HHH-10278 String systemLineSeparator = System.getProperty("line.separator"); System.setProperty("line.separator", ';' + systemLineSeparator); // get a persistence provider from spring context entityManagerFactory.getJpaVendorAdapter().getPersistenceProvider().generateSchema(entityManagerFactory.getPersistenceUnitInfo(), persistenceProperties); System.setProperty("line.separator", systemLineSeparator); System.exit(0); } } 
+1
source share

All Articles