如何在Liquibase中引用持久性单元名称

时间:2013-06-13 16:10:46

标签: java jpa database-migration liquibase persistence.xml

我想在现有数据库和JPA注释中定义的实体之间执行liquibase:diff。 实际上,我没有使用persistence.xml来定义entityManagerFactory,而是使用Spring和基于Java的配置:

    @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;
  }

实体在com.mycompany.entities中定义,我想在liquibase中引用我的持久性单元,以便它可以进行差异。

<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>

我尝试按照博文中的建议使用liquibase-hibernate扩展程序:http://thecliftonian.wordpress.com/2012/04/05/liquibase-2-0-3-with-hibernate-3-5-6-and-annotations/ 通过更新referenceUrl:<referenceUrl>persistance:my-persistance-unit</referenceUrl>但我有这个例外:

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

所以我的问题是如何在Liquibase中引用持久性单元名称?

有没有更好的方法来实现这一目标?

更新:referenceURl的javadoc说:

  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."

1 个答案:

答案 0 :(得分:1)

你引用的blob帖子看起来好像需要修补版本的集成,但是当他们的pull请求被带入liquibase-hibernate代码库时,我不相信它已成为一个发布,所以你需要自己做一个自定义的插件版本。