Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate LiquiBase + Spring + Hibernate without using hibernate.xml configuration file

I already read a lot of posts regarding integration between LiquiBase, Spring and Hibernate, but none of them apply to my situation witch is:

I'm starting a new project that uses Spring and Hibernate, so I was looking into a way to manage the database changes during the project lifetime. First I started using hbm2ddl but then realized that people say that this isn't a very good idea in production environments, so I came to the conclusion that LiquiBase was the way to go (so I think).

The problem is that I'm not using a hibernate.xml config file (and all the examples I found using LiquiBase use a hibernate.xml), since I'm using java annotations in my POJO/DB classes and my hibernate configuration is made like this

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() throws ClassNotFoundException
{
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
    entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);

    Properties jpaProterties = new Properties();
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
    jpaProterties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO));

    entityManagerFactoryBean.setJpaProperties(jpaProterties);

    return entityManagerFactoryBean;
}

I also found posts from 2 years ago saying that this option would only be available in version 2.0 (the current one), and I was wondering if this is already implemented. If so, how do I use it in a ANT script?

I need to create the original database DDL and the following database change logs and import them into the production DB.

EDIT : I'm using:

Liquibase 2.0.5

Liquibase Hibernate 2.0.0

Hibernate 4.1.4

Spring 3.1.1

Spring Data JPA 1.1.1

like image 962
Gonçalo Cardoso Avatar asked Aug 23 '12 10:08

Gonçalo Cardoso


People also ask

Does Liquibase use hibernate?

Hibernate can be used with several databases that are supported by Liquibase, such as H2. To use an H2 database with Liquibase, you must have the H2 JDBC driver JAR file., which is pre-installed with Liquibase. For more information, see Adding and Updating Liquibase Drivers.

How does Liquibase spring boot work?

The Liquibase Spring Boot integration ensures the application database is updated along with the application code by using Spring Boot auto-configuration and features. To use Liquibase and Spring Boot: Install Maven and add it to your path. Ensure you have Java Development Kit (JDK 8, 11, or 16).

Is Liquibase an ORM?

Hibernate and Liquibase are primarily classified as "Object Relational Mapper (ORM)" and "Database" tools respectively.


1 Answers

Did you look at http://www.liquibase.org/documentation/spring.html?

It mentions dataSource and no hibernate.xml.

For initial generation you can use command line mode and generateChangeLog.

See http://www.liquibase.org/documentation/command_line.html.

Here is a minimal hibernate.xfg.xml you're going to need:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
                "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="entityManagerFactoryBean">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3307/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    </session-factory>
</hibernate-configuration>
like image 81
Vadzim Avatar answered Oct 05 '22 20:10

Vadzim