Java Hibernate,Liquibase支持跨数据库和SQLite? [英] Java Hibernate, Liquibase supports cross databases and SQLite?

查看:512
本文介绍了Java Hibernate,Liquibase支持跨数据库和SQLite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Hibernate 5中使用Spring Boot 1.5.2并尝试支持尽可能多的数据库(即:Hibernate将创建SQLite 3中的所有表,然后使用Liquibase作为抽象层来生成XML更改-log文件,用于Liquibase声称的所有支持的数据库:支持的数据库)。



所以我在pom.xml(Maven)中添加了Liquibase的依赖项。

 <! - 数据库模式版本迁移 - > 


< dependency>
< groupId> org.liquibase< / groupId>
< artifactId> liquibase-core< / artifactId>
< version> 3.5.3< / version>
< /依赖关系>
< dependency>
< groupId> org.liquibase< / groupId>
< artifactId> liquibase-maven-plugin< / artifactId>
< version> 3.4.1< / version>
< /依赖关系>

以及一个用于从创建的Hibernate数据库生成更改日志XML文件的插件

 <! - 数据库模式版本迁移 - > 

< plugin>
< groupId> org.liquibase< / groupId>
< artifactId> liquibase-maven-plugin< / artifactId>
< version> 3.5.3< / version>
<配置>
< propertyFile> src / main / resources / liquibase.properties< / propertyFile>
< / configuration>
< / plugin>

我在liquibase.properties中有一些配置来连接到SQLite3数据文件,然后我可以运行这个命令创建更改日志文件。

  mvn liquibase:generateChangeLog 

由于 addPrimaryKey 元素的原因,输出更改日志我无法在另一个不同的SQLite 3数据库文件中重新创建:

 < changeSet author =rasdaman(generated)id =1497363976895-86> 
< addPrimaryKey columnNames =address_idtableName =address/>
< / changeSet>

以及Java启动时的错误:

  sqlite不支持addPrimaryKey,
classpath:/database_versions/db.changelog-master.xml :: 1497366115846-62 :: rasdaman(生成)

at liquibase.changelog.DatabaseChangeLog.validate(DatabaseChangeLog.java:266)
at liquibase.Liquibase.update(Liquibase.java:210)
at liquibase.Liquibase.update(Liquibase .java:192)
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431)
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388)
。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)

I f我使用Liquibase从SQLite生成的输出文件,并允许Spring Boot以Postgresql数据源开头,但我得到另一个错误:

  org.springframework.beans.factory.BeanCreationException:在类路径资源


[org / springframework / boot / autoconfigure / liquibase /中定义名称'liquibase' LiquibaseAutoConfiguration $ LiquibaseConfiguration.class]:调用init方法失败;嵌套异常liquibase.exception.MigrationFailedException:迁移失败,更改set classpath:/database_versions/db.changelog-master.xml :: 1497366115846-1 ::(生成):
原因:liquibase.exception.DatabaseException:ERROR :语法错误在或靠近(
位置:72 [失败的SQL:CREATE TABLE public。HT_abstract_coverage(abstract_coverage_id BIGINT(2000000000,10)NOT NULL,hib_sess_id CHAR(36))]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)

看起来我没有其他工具可以自动生成像XML这样的抽象数据文件,它可以导入到任何常见的数据库中而不会出现任何问题?如果您有任何建议,请告知!



谢谢,

解决方案

问题是Liquibase不太支持SQLite http://www.liquibase.org/documentation/changes/add_primary_key.html ,同样需要第三方SQLite方言和Hibernate的性能写不好,所以我认为Liquibase可以很好地与其他数据库如postgresql,mysql,hyperSQL ...一起工作。


I'm using Spring Boot 1.5.2 with Hibernate 5 and try to support as many databases as possible (i.e: Hibernate will create all the tables in SQLite 3, then I use Liquibase as an abstract layer to generate the XML change-log files for all kind of supported databases which Liquibase claimed: supported databases).

so I added the dependency for Liquibase in pom.xml (Maven).

    <!-- Database schema versions migration -->


    <dependency>
        <groupId>org.liquibase</groupId>
         <artifactId>liquibase-core</artifactId>
          <version>3.5.3</version>
    </dependency>
    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>
        <version>3.4.1</version>
    </dependency>

and a plugin to generate the changelog XML file from created database of Hibernate

<!-- Database schema versions migration -->  

 <plugin>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>
        <version>3.5.3</version>
        <configuration>                  
            <propertyFile>src/main/resources/liquibase.properties</propertyFile>
        </configuration>                
 </plugin> 

I have some configuration in liquibase.properties to connect to SQLite3 datab file, then I can run this command to create a changelog file.

mvn liquibase:generateChangeLog

The output changelog I cannot recreate in another different SQLite 3 db file, due to the addPrimaryKey element:

<changeSet author="rasdaman (generated)" id="1497363976895-86">
     <addPrimaryKey columnNames="address_id" tableName="address"/>
</changeSet>

and the error in Java Spring Boot when it starts:

addPrimaryKey is not supported on sqlite, 
classpath:/database_versions/db.changelog-master.xml::1497366115846-62::rasdaman (generated)

    at liquibase.changelog.DatabaseChangeLog.validate(DatabaseChangeLog.java:266)
    at liquibase.Liquibase.update(Liquibase.java:210)
    at liquibase.Liquibase.update(Liquibase.java:192)
    at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431)
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)

If I use the generated output file from SQLite by Liquibase and allow Spring Boot to starts with Postgresql datasource, I got another error:

org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'liquibase' defined in class path resource     


[org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set classpath:/database_versions/db.changelog-master.xml::1497366115846-1:: (generated):
         Reason: liquibase.exception.DatabaseException: ERROR: syntax error at or near "("
      Position: 72 [Failed SQL: CREATE TABLE public."HT_abstract_coverage" (abstract_coverage_id BIGINT(2000000000, 10) NOT NULL, hib_sess_id CHAR(36))]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)

So it seems I don't have another tool to generate an abstract data file like XML automatically which can be imported to any common databases without problems? If you have any suggestion, please advise!

Thanks,

解决方案

The problem is Liquibase does not support SQLite very well http://www.liquibase.org/documentation/changes/add_primary_key.html , also Hibernate which needs a third party SQLite dialect and the performance for write is not good, so I think Liquibase could work well with another databases like: postgresql, mysql, hyperSQL,...

这篇关于Java Hibernate,Liquibase支持跨数据库和SQLite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆