0

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();
        }
    }
}
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
Alexey Galanov
  • 421
  • 4
  • 21

1 Answers1

0

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)

P.S: 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

vexan
  • 88
  • 4
  • hibernate.hbm2ddl.auto=update is bad idea http://stackoverflow.com/questions/221379/hibernate-hbm2ddl-auto-update-in-production. java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files - we are developing dynamically extensible, so it's not an option – Alexey Galanov Jun 06 '16 at 13:40
  • may be this link will be helpful http://www.baeldung.com/liquibase-refactor-schema-of-java-app – vexan Jun 21 '16 at 21:17