简体   繁体   中英

Liquibase hibernate tables update from java code

Could you please give me some examples of how to update database tables from java code using hibernate entity classes and liquibase.

Something like this, but for annotated classes

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();
        }
    }
}

You can't do that because of liquibase knows nothing about hibernate.

But you can update database with hibernate by set property

hibernate.hbm2ddl.auto=update

or you can generate ddl into file

java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files

with this tool you can export ddl to a file and execute script manually or do it automatically (mapping_files is optional)

PS: liquibase is developed for declarative markup of updates, not for using with hibernate together

may be this link will be helpful

http://www.baeldung.com/liquibase-refactor-schema-of-java-app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM