2

Till now, I have used liquibase with spring and standard changelog file. Now I want to integrate it to hibernate so when I do any change to entity, it should change tables and update changelog table.

I read many examples, and tutorials and can't get it work.

I use datasource, entityManagerFactory(denepds-on="liquibase") all work fine. But when I add liquibase bean it doesn't change database from entity. And "no table with name" exception is thrown.

How to configure Liquibase with liquibase-hibernate4 and spring?

my liquibase bean:

<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="myJdbcDataSource" />
      <property name="beanName" value="entityManagerFactory" />
      <property name="changeLog" value="classpath:databaseChangeLog.sql" />
</bean>

I just added beanName prop as entityManagerFactory from: https://github.com/liquibase/liquibase-hibernate/wiki, http://www.liquibase.org/documentation/spring.html. And did what I can understand from this posts.

In this posts, it mentioned that we should use one of three variants

hibernate:spring:com/example/spring.xml?bean=sessionFactory
hibernate:spring:com.example?dialect=org.hibernate.dialect.MySQL5Dialect
hibernate:spring:com.example.employee,com.example.auction?dialect=org.hibernate.dialect.MySQL5Dialect

But I don't know how to use it with liquibase bean when we using spring

malaguna
  • 4,183
  • 1
  • 17
  • 33
Erlan
  • 2,010
  • 1
  • 22
  • 31

1 Answers1

5

I am using Spring, Liquibase nad Hibernate for years, and I do not recommend you using it as you want. My experience tells me that it is not a good a idea to let Hibernate to change your database automatically.

I doesn't mind if the change is calculated by hibernate using hbm2ddl or by means of liquibase which in the end calculates database changes from hibernate. You would rather prefer to supervise that changes before to incorporate them to your changelog.

So, in my case, I prefer to configure liquibase (using maven) to write automatically a changelog obtained from comparing my hibernate mappings against database (I call it autochangelog). Then I check manually that changesets before merging it with my changelog.

Of course, you can use SpringLiquibase bean to automatically run an liquibase update every time you start your app, this works out of the box, but be careful when working when different environments, you could run an undesirable update ...

Maybe you could find this post of your interest http://malaguna.github.io/blog/2015/06/09/liquifying-your-project/

Here it is described how I use Spring, Hibernate and Liquibase as I told you.

malaguna
  • 4,183
  • 1
  • 17
  • 33
  • Thanks @malaguna, now I see the way. But for the curiousity, is it possible to do as I asked(configure and run liquibase diff, update when spring started). If yes, how can we achieve that? – Erlan Jan 11 '16 at 04:04
  • I am afraid you can't, at least as you want it. You could set `diffChangeLogFile` in `liquibase.properties` to point directly to your changelog (*I don't recommend you*) This way you get directly your `diffs` into your `changelog`. But Spring Liquibase bean does not calculate diffs, I think it is only intended to run `update` liquibase command. You need to run `liquibase diffs` from maven, eclipse task or command line. – malaguna Jan 11 '16 at 06:58
  • Hmm. I thought we could do diff too on SpringLiquibase. Thanks for the answer – Erlan Jan 11 '16 at 11:26