![](/img/trans.png)
[英]spring.jpa.hibernate.ddl-auto=update not working in spring boot
[英]Spring boot and Hibernate: print/log DDL
在我添加了一個或多個帶有數據庫映射(JPA / hibernate)的類之后,我希望Hibernate打印出必要的模式更新,以便我可以在數據庫上執行它們(例如通過FlyWay)。 我不希望更新自動執行。
唯一似乎對此有一定控制權的屬性如下
org.hibernate.tool.hbm2ddl=validate|update|create|create-drop|none
我不想自動更新/更改任何內容。 我想將此設置為驗證或無。 當我這樣做時,我無法看到生成的架構。
我是經典的spring應用程序,我曾經使用hibernate SchemaExport
類來打印DDL。
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.execute(true, false, false, false);
我可以在Spring Boot中使用類似的東西嗎?
這就是我做的......
首先,我對我的實體進行了更改,然后將其設置為:
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
然后
即使打開調試,設置show-sql的解決方案對我來說也不起作用,所以我最終編寫並運行這個簡單的類。
public class SchemaExporter{
public static org.hibernate.cfg.Configuration getConfiguration() {
org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
for (BeanDefinition bd : scanner.findCandidateComponents("com.package.where.my.entitybeans.are")) {
String name = bd.getBeanClassName();
try {
System.out.println("Added annotated entity class " + bd.getBeanClassName());
cfg.addAnnotatedClass(Class.forName(name));
} catch (Exception e) {
e.printStackTrace();
}
}
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
cfg.setProperty("hibernate.show_sql", "true");
cfg.setProperty("hibernate.format_sql", "true");
cfg.setProperty("hibernate.hbm2ddl.auto", "update");
cfg.setProperty("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
cfg.setProperty("hibernate.connection.url", CONNECTIONURL);
cfg.setProperty("hibernate.connection.username", USERNAME);
cfg.setProperty("hibernate.connection.password", PWD);
cfg.setProperty("hibernate.connection.driver", DRIVER);
return cfg;
}
public static void main(String[] args) {
SchemaExport export = new SchemaExport(getConfiguration());
export.setDelimiter(";");
export.setHaltOnError(true);
export.setFormat(true);
export.create(true,true);
}
}
運行它我可以在控制台中看到DDL並繼續克里斯建議
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.