0

I want to perform a liquibase:diff between an existing database and my entities defined in JPA annotations. Actually, instead of using a persistence.xml to define the entityManagerFactory, I am using Spring with Java based configuration as such :

    @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory()
  {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setDataSource(dataSource());
    entityManagerFactory.setPersistenceUnitName("my-persistence-unit");
    entityManagerFactory.setPackagesToScan("com.mycompany.entities");
    entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());


    Properties properties = new Properties();
    properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
    properties.put("hibernate.hbm2ddl.auto", "validate");
    properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    properties.put("hibernate.connection.url", "jdbc:h2:D:/work/06-database/my-database;");
    properties.put("hibernate.format_sql", "true");
    properties.put("hibernate.connection.username", "sa");
    properties.put("hibernate.connection.password", "");   

    entityManagerFactory.setJpaProperties(properties); 

    return entityManagerFactory;
  }

The entities are defined in com.mycompany.entities and I want to reference my persistence unit in liquibase so it can make the diff.

<plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.0.0-rc1</version>
            <configuration>
                <changeLogFile>src/main/resources/changelog/changelog-master.xml</changeLogFile>
                <diffChangeLogFile>src/main/resources/db/migration/changelog-${project.version}.xml</diffChangeLogFile>
                <driver>org.h2.Driver</driver>
                <url>jdbc:h2:D:/work/06-database/my-database;</url>                 
                <referenceUrl>src/main/resources/META-INF/persistence.xml</referenceUrl>
                <referenceDriver>org.h2.Driver</referenceDriver>                    
                <username>sa</username>
                <password></password>
            </configuration>                
        </plugin>

I tried using liquibase-hibernate extension as suggested in the blog post : http://thecliftonian.wordpress.com/2012/04/05/liquibase-2-0-3-with-hibernate-3-5-6-and-annotations/ by updating the referenceUrl : <referenceUrl>persistance:my-persistance-unit</referenceUrl> but I have this exception :

Error setting up or running Liquibase: liquibase.exception.DatabaseException: Connection could not be created to src/main/resources/META-INF/persistence.xml with driver org.h
2.Driver.  Possibly the wrong driver for the given database URL

So my question is how to How to reference a persistence unit name in Liquibase?

Is there a better way to achieve that?

Update : The javadoc of the referenceURl says :

  The reference database URL to connect to for executing Liquibase. If performing a diff
 against a Hibernate config xml file, then use <b>"hibernate:PATH_TO_CONFIG_XML"</b>
  as the URL. The path to the hibernate configuration file can be relative to the test
  classpath for the Maven project."
Dimitri
  • 8,122
  • 19
  • 71
  • 128

1 Answers1

2

The blob post you referenced looks like they have a work-around requiring a patched version of the integration, but while their pull request has been brought into the liquibase-hibernate codebase, I don't believe it has made it into a release yet so you will need to do a custom build of the plugin yourself.

Nathan Voxland
  • 15,453
  • 2
  • 48
  • 64
  • Hi, I update my question in http://forum.liquibase.org/topic/how-to-reference-a-persistence-unit-name-in-liquibase#49382000000741089 – Dimitri Jun 17 '13 at 10:00