[英]Liquibase hibernate tables update from java code
您能否舉一些例子,說明如何使用休眠實體類和liquibase從Java代碼更新數據庫表。
像這樣,但用於帶注釋的類
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/dvdrental", "postgres", "admin");
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new Liquibase("C:/changelog.xml", new FileSystemResourceAccessor(), database);
liquibase.update(new Contexts());
} catch (SQLException | LiquibaseException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.rollback();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
您不能這樣做,因為liquibase對冬眠一無所知。
但是您可以通過set屬性使用休眠模式更新數據庫
hibernate.hbm2ddl.auto=update
或者您可以將ddl生成到文件中
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files
使用此工具,您可以將ddl導出到文件並手動執行腳本或自動執行腳本(mapping_files是可選的)
PS:liquibase是為聲明性更新標記而開發的,不適用於與hibernate一起使用
可能這個鏈接會有所幫助
http://www.baeldung.com/liquibase-refactor-schema-of-java-app
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.