1

If I add to my spring-boot / hibernate app an entity with secondary table, defined e.g. like this (taken from 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

}

and I run liquibase diff command to see what is the difference between entities in code and the underlying database (PostgreSQL or MySQL - it does not matter), I get only one change set:

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

for table meal but nothing for table allergens!

It looks like Liquibase knows that allergens columns belong to another (secondary) table, but does not create changeSet for it. Is it a bug in Liquibase?

If I let Hibernate generate the tables, it correctly creates also secondary allergens table in database. If I now run diff command second time, it generates changeSet which is about to delete allergens table from database - that`s bad. Any ideas how to force Liquibase to recognize the secondary table?

My Maven profile used for running the diff is:

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

and liquibase-postgresql.properties is:

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

Here: https://liquibase.jira.com/browse/CORE-3029 it seems to be the same problem, but not answered...

Thanks for any advice.

0 Answers0