Liquibase差异不适用于@SecondaryTable

时间:2020-04-01 09:27:44

标签: spring hibernate diff liquibase

如果我将带有辅助表的实体添加到spring-boot / hibernate应用程序中,例如像这样(摘自https://www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables):

@Entity
@Table(name = "meal")
@SecondaryTable(name = "allergens", pkJoinColumns = @PrimaryKeyJoinColumn(name = "meal_id"))
class Meal {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    Long id;

    @Column(name = "name")
    String name;

    @Column(name = "description")
    String description;

    @Column(name = "price")
    BigDecimal price;

    @Embedded
    Allergens allergens;

    // standard getters and setters

}

@Embeddable
class Allergens {

    @Column(name = "peanuts", table = "allergens")
    boolean peanuts;

    @Column(name = "celery", table = "allergens")
    boolean celery;

    @Column(name = "sesame_seeds", table = "allergens")
    boolean sesameSeeds;

    // standard getters and setters

}

然后我运行liquibase diff命令来查看代码中的实体和基础数据库(PostgreSQL或MySQL-没关系)之间的区别是什么,我只得到一个更改集:

<changeSet author="jakub (generated)" id="1585730629323-1">
   <createTable tableName="meal">
        <column autoIncrement="true" name="id" type="BIGINT">
             <constraints nullable="false" primaryKey="true" primaryKeyName="mealPK"/>
        </column>
        <column name="description" type="VARCHAR(255)"/>
        <column name="name" type="VARCHAR(255)"/>
        <column name="price" type="numeric(19, 2)"/>
   </createTable>
</changeSet>
meal

,表allergens

看起来Liquibase知道allergens列属于另一个(辅助)表,但没有为其创建changeSet。这是Liquibase中的错误吗?

如果让Hibernate生成表,它将在数据库中正确创建辅助allergens表。如果我现在第二次运行diff命令,它将生成changeSet,该更改将要从数据库中删除allergens表-这很糟糕。有什么想法可以迫使Liquibase识别辅助表吗?

用于运行差异的我的Maven配置文件是:

       <profile>
            <id>db-diff-postgresql</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.liquibase</groupId>
                        <artifactId>liquibase-maven-plugin</artifactId>
                        <version>3.8.7</version>
                        <executions>
                            <execution>
                                <id>generate-db-diff-postgresql</id>
                                <phase>process-test-resources</phase>
                                <goals>
                                    <goal>diff</goal>
                                </goals>
                                <configuration>
                                    <propertyFile>src/main/resources/db/liquibase-postgresql.properties</propertyFile>
                                    <diffChangeLogFile>src/main/resources/db/changelogs/postgresql/changelog_diff_postgresql_${maven.build.timestamp}.xml</diffChangeLogFile>
                                    <logging>debug</logging>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

liquibase-postgresql.properties是:

changeLogFile= src/main/resources/db/db.changelog.xml
driver= org.postgresql.Driver
url= jdbc:postgresql://localhost/myApp
username= postgres
password= password
referenceUrl= hibernate:spring:myapp.jpa.entity?dialect=org.hibernate.dialect.PostgreSQLDialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy

这里:https://liquibase.jira.com/browse/CORE-3029似乎是同样的问题,但没有得到答案...

谢谢您的建议。

0 个答案:

没有答案
相关问题