-
Notifications
You must be signed in to change notification settings - Fork 160
Home
This Liquibase extension lets you use your Hibernate configuration as a comparison database for diff, diffChangeLog and generateChangeLog.
- Edit your Hibernate mapped classes as needed (add and remove classes and attributes)
- Run liquibase --changeLogFile=changelog.xml --url=jdbc:yourdatabase --referenceUrl=hibernate:classic:path/to/hibernate.cfg.xml diffChangeLog
- Check that the modified changelog.xml does what you expect, edit it if it does not
- Run liquibase --changeLogFile=changelog.xml --url=jdbc:yourdatabase update
- Repeat
If you specify an existing changeLogFile, each run of diffChangeLog will append to the file.
The main problem with HBM2DDL is that it automatically applies changes that it thinks is right. For many cases that can be fine, but as with any diff-based database tool it can easily destroy data because it matters HOW the database gets from state A to state B. For example, if you rename your Person class to Employee, hbm2ddl will just create a new table called EMPLOYEE rather than perform the rename you want.
What makes Liquibase different is that it does not figure out changes based on schema comparisons but instead runs the changes you said need to be made. The Liquibase Hibernate extension makes creating those changes easier by automatically creating a rough draft of those changes. Most of the time the changes can be run directly, but in the cases where it guesses wrong you have the opportunity to fix the proposed changes before they are applied to your database and all other databases down the pipeline.
Once installed, the liquibase-hibernate integration allows you to treat your hibernate configuration and classes like any other database by using a special connection url syntax.
Three styles of connection URLs are supported:
The hibernate classic URL allows you to either reference your hibernate config XML file or specify a class name that implements liquibase/ext/hibernate/customfactory/CustomClassicConfigurationFactory
Database URL Examples:
hibernate:classic:com/example/hibernate.cfg.xml
hibernate:classic:hibernate.cfg.xml
hibernate:classic:com.example.MyConfigFactory
hibernate:classic:com/example/hibernate.cfg.xml?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
The hibernate ejb3 URL allows you to either reference a persistence-unit defined in your persistence.xml or specify a class name that implements liquibase/ext/hibernate/customfactory/CustomEjb3ConfigurationFactory
Database URL Examples:
hibernate:ejb3:myPersistenceUnit
hibernate:ejb3:com.example.MyConfigFactory
hibernate:ejb3:myPersistenceUnit?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
The hibernate spring URL allows you to either reference your a bean within a spring config or specify a comma separated list of package containing hibernate or javax.persistence annotated classes to use. Spring is a required dependency, even if you are listing a package and not a spring class.
- If you are defining a spring config file, bean is a required URL parameter.
- If you are defining a package to scan, dialect is a required URL parameter.
Database URL Examples:
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
For all url styles, you can specify hibernate parameters using URL-style format such:
hibernate:classic:com/example/hibernate.cfg.xml?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
Any properties defined in the URL will override parameters set in the standard configuration file(s).
Simply download the liquibase-hibernate jar from the release page and add it to your classpath. You can also add a dependency through Maven (group: org.liquibase.ext, artifact liquibase-hibernate3 or liquibase-hibernate4)
The Liquibase-Hibernate extension requires Liquibase 3.0.6+ and Java 1.6+. Use liquibase-hibernate3.jar or liquibase-hibernate4.jar depending on your hibernate version.
Use the issue tracker to log any issues or feature requests
Pull requests are greatly appreciated