8

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

Gonçalo Cardoso
  • 2,253
  • 4
  • 34
  • 63

1 Answers1

3

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>
vbence
  • 20,084
  • 9
  • 69
  • 118
Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • Yes, I already looked at both of those examples, but my problem is what parameters to pass exactly, because I want LiquiBase to retrieve the DB changes from my POJO/DB annotated classes and not from my DB. – Gonçalo Cardoso Aug 23 '12 at 14:54
  • 1
    Ok. Now I get the question. Look at http://stackoverflow.com/questions/776787/hibernate-using-jpa-annotated-entities-and-liquibase. It's complete answer. You'll just need dummy hibernate.cfg.xml. – Vadzim Aug 23 '12 at 15:18
  • What do you mean by a dummy hibernate.cfg.xml ? – Gonçalo Cardoso Aug 24 '12 at 09:41
  • Here are my files: hibernate.cfg.xml - http://pastebin.com/6Jeuwq8B ANT - http://pastebin.com/tcCGQx03 ApplicationContext.java http://pastebin.com/480cDi40 – Gonçalo Cardoso Aug 24 '12 at 10:41
  • And what is the concrete problem with them? What exactly fails? – Vadzim Aug 24 '12 at 14:23
  • My changelog.xml file is empty. Don't I need to LiquiBase where my POJO/DB annotated classes are located? If so how? Or does it scan all the class folder? – Gonçalo Cardoso Aug 24 '12 at 14:26
  • Try adding `mapping class` line of each pojo. See http://stackoverflow.com/questions/1413190/hibernate-mapping-package. – Vadzim Aug 24 '12 at 14:39
  • D:\Workspaces\Rastreability\EAR Build.xml:373: liquibase.exception.DatabaseException: org.hibernate.MappingException: Unable to load class [ com.db.T_AppConfig] declared in Hibernate configuration entry – Gonçalo Cardoso Aug 24 '12 at 15:00
  • Replace `pathelement location="WebContent/WEB-INF/classes/com/xpto/db"` with `pathelement path`. – Vadzim Aug 24 '12 at 15:11
  • That did kind of the trick. It created the file but not with all the DB info. Gonna take a closer look and get to you about my conclusions ;) – Gonçalo Cardoso Aug 24 '12 at 16:27
  • As you said, I added this to my ANT file And my hibernate.cfg.xml looks like this ... – Gonçalo Cardoso Aug 27 '12 at 11:26