bal*_*teo 5 hibernate jpa hibernate-tools gradle
我试图设置一个gradle任务,它运行一个用于生成SQL模式的java主类.
我没有persistence.xml配置文件.
这是我的配置和代码:
我的gradle任务:
task JpaSchemaExport(type: JavaExec){
description "Exports Jpa schema"
dependsOn compileJava
main = "com.bignibou.tools.jpa.JpaSchemaExport"
classpath = sourceSets.main.runtimeClasspath + configurations.compile
}
Run Code Online (Sandbox Code Playgroud)
我的出口效用:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的数据配置:
@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;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的例外:
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)
Run Code Online (Sandbox Code Playgroud)
编辑:我确实得到了警告:
: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
Run Code Online (Sandbox Code Playgroud)
JpaSchemaExport据我从您的代码中看到,您的内容不是在 Spring 上下文中执行的。
当您JpaSchemaExport从 Gradle 或命令行运行时,您会创建自己的文件EntityManager,与 Spring 无关,并persistence.xml在META-INF目录中查找文件。对于 Spring Boot 应用程序,不需要此文件,因此可能不存在。
当我运行看起来与 JpaSchemaExport 类似的东西时,输出类似于
[主要] INFO ohjbiPersistenceXmlParser - HHH000318:在类路径中找不到任何 META-INF/persistence.xml 文件
[main] DEBUG ohjpa.HibernatePersistenceProvider - 定位并解析 0 个持久单元;检查每个
[main] 调试 ohjpa.HibernatePersistenceProvider - 在线程“main”中找不到匹配的持久性单元异常
javax.persistence.PersistenceException:找不到持久性提供程序
用于名为 default 的持久性单元的模式生成
javax.persistence.Persistence.generateSchema(Persistence.java:93) 在
com.example.springboot.jpa.JpaSchemaExport.execute(JpaSchemaExport.java:42)
在
com.example.springboot.jpa.JpaSchemaExport.main(JpaSchemaExport.java:14)
Spring Boot 命令行应用程序(具有 Spring 上下文的应用程序)看起来像:
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Override
public void run(String... strings) throws Exception {
}
}
Run Code Online (Sandbox Code Playgroud)
即你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
}
}
Run Code Online (Sandbox Code Playgroud)
作为替代方案,您可以创建一个META-INF/persistence.xml可供您的 Spring Boot 应用程序以及您的JpaSchemaExport.
| 归档时间: |
|
| 查看次数: |
3381 次 |
| 最近记录: |