Liquibase差异生成错误的主键

时间:2018-09-02 23:23:06

标签: java maven spring-boot liquibase

当我使用liquibase在具有@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)的实体上生成我的变更集时,我遇到了问题

pom.xml

<plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.6.2</version>
            <configuration>
                <!--<propertyFile>src/main/resources/liquibase.properties</propertyFile>-->
                <username>synchroad_adm</username>
                <password>changeme</password>
                <url>jdbc:postgresql://localhost:5432/synchroad?currentSchema=synchroad_adm</url>
                <referenceDefaultSchemaName>synchroad_adm</referenceDefaultSchemaName>
                <defaultSchemaName>synchroad_adm</defaultSchemaName>
                <referenceUrl>hibernate:spring:com.demo?dialect=org.hibernate.dialect.PostgreSQLDialect</referenceUrl>
                <driver>org.postgresql.Driver</driver>
                <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                <changeLogFile>${project.basedir}/src/main/resources/db/liquibase/app/db.changelog.xml</changeLogFile>
                <diffChangeLogFile>target/changelog-diff.xml</diffChangeLogFile>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.liquibase.ext</groupId>
                    <artifactId>liquibase-hibernate5</artifactId>
                    <version>3.6</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                    <version>2.0.4.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>javax.validation</groupId>
                    <artifactId>validation-api</artifactId>
                    <version>2.0.1.Final</version>
                </dependency>
                <dependency>
                    <groupId>org.javassist</groupId>
                    <artifactId>javassist</artifactId>
                    <version>3.23.1-GA</version>
                </dependency>
            </dependencies>
        </plugin>

java类

@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class MyTable {

    @Id
    @TableGenerator(name = "agent_generator", table = "agent_ids", pkColumnName = "seq_id", valueColumnName = "seq_value")
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "agent_generator")
    @Column(name = "id")
    private Long id;
}

@Entity
@Table(name = "table_one")
public class TableOne extends MyTable {

}

@Entity
@Table(name = "table_two")
public class TableTwo extends MyTable {

}

我正在使用Liquibase生成我的变更集。

mvn clean install liquibase:diff 

但是Liquibase生成2个具有相同primaryKeyName的createTable

diff.xml

<changeSet author="mvallet (generated)" id="1535684993363-2">
  <createTable tableName="table_one">
    <column name="id" type="BIGINT">
      <constraints primaryKey="true" primaryKeyName="MyTablePK"/>
    </column>
  </createTable>
</changeSet>
<changeSet author="mvallet (generated)" id="1535684993363-2">
  <createTable tableName="table_two">
    <column name="id" type="BIGINT">
      <constraints primaryKey="true" primaryKeyName="MyTablePK"/>
    </column>
  </createTable>
</changeSet>

所以当我启动

mvn clean install liquibase:update

我有一个错误错误:错误:关系“ MyTablePK”已经存在。

如果我不使用liquibase,但让我休眠以生成表,那我没有问题。

有人知道吗?

1 个答案:

答案 0 :(得分:0)

尝试从MyTable类中删除@Entity 如果您使用休眠模式,则生成的PKN是什么? 为什么您需要保留MyTable?