开始时,问题仅在于H2数据库.在其他数据库(MySQL)上,它可以正常工作而没有问题.
由(jHipster)生成的配置:
个人资料:dev
spring:
profiles:
active: dev
datasource:
dataSourceClassName: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
url: jdbc:mysql://******.net.net:3306/*****_pandemicd?autoReconnect=true
databaseName: ****************
serverName: ******.net
username: ****************
password: ****************
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: MYSQL
openInView: true
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: pl.sedzisz.pandemic.config.hazelcast.HazelcastCacheRegionFactory
hibernate.cache.use_minimal_puts: true
hibernate.cache.hazelcast.use_lite_member: true
配置文件:测试(在内存中)
spring:
datasource:
dataSourceClassName: org.h2.jdbcx.JdbcDataSource
url: jdbc:h2:mem:pandemic;DB_CLOSE_DELAY=-1;MODE=MySQL;TRACE_LEVEL_FILE=4
databaseName:
serverName:
username:
password:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
database: H2
openInView: true
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
hibernate.hbm2ddl.auto: validate
现在,Liquibase更改日志:
<changeSet author="newbie" id="1449941926339-1">
<createTable tableName="ENUM_DESCENT">
<column name="descent_of_disease" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet author="newbie" id="1449941926339-2">
<loadData encoding="UTF-8"
file="config/liquibase/descent_of_disease.csv"
separator=";"
tableName="ENUM_DESCENT"/>
</changeSet>
<changeSet author="newbie" id="1449941926339-3">
<createTable tableName="DISEASE_DESCENTS">
<column name="disease_id" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="descent_descent_of_disease" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet author="newbie" id="1449941926339-4">
<addPrimaryKey columnNames="disease_id, descent_descent_of_disease"
constraintName="PRIMARY"
tableName="DISEASE_DESCENTS"/>
</changeSet>
<changeSet author="newbie" id="1449941926339-5">
<addPrimaryKey columnNames="descent_of_disease"
constraintName="PRIMARY"
tableName="ENUM_DESCENT"/>
</changeSet>
和日志示例:
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Executing EXECUTE database command: ALTER TABLE PUBLIC.DISEASE_DESCENTS ADD CONSTRAINT "PRIMARY" PRIMARY KEY (disease_id, descent_descent_of_disease)
[INFO] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Primary key added to DISEASE_DESCENTS (disease_id, descent_descent_of_disease)
[INFO] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: ChangeSet config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie ran successfully in 1ms
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Computed checksum for addPrimaryKey:[
columnNames="disease_id, descent_descent_of_disease"
constraintName="PRIMARY"
tableName="DISEASE_DESCENTS"
] as eb8eba06bfcf2a30a029b8e49ea7d549
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Computed checksum for 7:eb8eba06bfcf2a30a029b8e49ea7d549: as 212a7c01d41bbcb1f9772a2d93161529
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Executing EXECUTE database command: INSERT INTO PUBLIC.DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, DESCRIPTION, COMMENTS, EXECTYPE, LIQUIBASE) VALUES ('1449941926339-4', 'newbie', 'config/liquibase/changelog/20151115220815_added_entity_Descent.xml', NOW(), 6, '7:212a7c01d41bbcb1f9772a2d93161529', 'addPrimaryKey', '', 'EXECUTED', '3.3.2')
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Computed checksum for addPrimaryKey:[
columnNames="disease_id, descent_descent_of_disease"
constraintName="PRIMARY"
tableName="DISEASE_DESCENTS"
] as eb8eba06bfcf2a30a029b8e49ea7d549
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Computed checksum for 7:eb8eba06bfcf2a30a029b8e49ea7d549: as 212a7c01d41bbcb1f9772a2d93161529
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Running Changeset:config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Changeset config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Reading ChangeSet: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Executing Statement: liquibase.statement.core.AddPrimaryKeyStatement@7334b84d
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Executing EXECUTE database command: ALTER TABLE PUBLIC.ENUM_DESCENT ADD CONSTRAINT "PRIMARY" PRIMARY KEY (descent_of_disease)
[ERROR] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Change Set config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie failed. Error: org.h2.jdbc.JdbcSQLException: Ograniczenie "PRIMARY" już istnieje
Constraint "PRIMARY" already exists; SQL statement: ALTER TABLE PUBLIC.ENUM_DESCENT ADD CONSTRAINT "PRIMARY" PRIMARY KEY (descent_of_disease) [90045-187]
liquibase.exception.DatabaseException: org.h2.jdbc.JdbcSQLException: Ograniczenie "PRIMARY" już istnieje
Constraint "PRIMARY" already exists; SQL statement: ALTER TABLE PUBLIC.ENUM_DESCENT ADD CONSTRAINT "PRIMARY" PRIMARY KEY (descent_of_disease) [90045-187] at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:316) ~[liquibase-core-3.3.2.jar:na] at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55) ~[liquibase-core-3.3.2.jar:na] at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:122) ~[liquibase-core-3.3.2.jar:na]
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1227) ~[liquibase-core-3.3.2.jar:na]
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1210) ~[liquibase-core-3.3.2.jar:na]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:550) ~[liquibase-core-3.3.2.jar:na]
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:43) [liquibase-core-3.3.2.jar:na]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:73) [liquibase-core-3.3.2.jar:na]
at liquibase.Liquibase.update(Liquibase.java:200) [liquibase-core-3.3.2.jar:na]
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:353) [liquibase-core-3.3.2.jar:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:317) [liquibase-core-3.3.2.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:444) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
解决方法:
<changeSet author="newbie" id="1449941926339-4">
<addPrimaryKey columnNames="disease_id, descent_descent_of_disease"
constraintName="PRIMARY_DISEASE_DESCENTS"
tableName="DISEASE_DESCENTS"/>
</changeSet>
<changeSet author="newbie" id="1449941926339-5">
<addPrimaryKey columnNames="descent_of_disease"
constraintName="PRIMARY_ENUM_DESCENT"
tableName="ENUM_DESCENT"/>
</changeSet>
MySQL: Creating table with two foreign keys fails with “Duplicate key name” error
解决方法:
如果在创建表之后添加了主键,liquibase将使用ALTER TABLE … ADD CONSTRAINT….而且您不能有多个具有相同名称的约束.因此H2报告了错误.
您已经找到了解决方法.
也许另一种方法是将主键定义添加到create table语句中:
<changeSet author="newbie" id="1449941926339-1">
<createTable tableName="ENUM_DESCENT">
<column name="descent_of_disease" type="VARCHAR(255)">
<constraints nullable="false"/>
<constraints primaryKey="true"/>
</column>
</createTable>
</changeSet>
然后,将在CREATE TABLE …语句中处理主键定义.
(顺便说一句:这在mySQL上不会发生,因为mySQL能够在ALTER TABLE …语句上使用PRIMARY KEY.因此,使用mySQL DB liquibase会将您的addPrimaryKey变更集转换为ALTER TABLE … ADD PRIMARY KEY. )