0

I have two entity Person and Address. And Person can have multiple Address.

<createTable tableName="ADDRESS">
            <column name="id" type="bigint(20)" autoIncrement="true">
                <constraints primaryKey="true" nullable="false" />
               ... //columns
            </column>
</createTable>

<createTable tableName="PERSON">
            <column name="id" type="bigint(20)" autoIncrement="true">
                <constraints primaryKey="true" nullable="false" />
               ... //columns
            </column>
</createTable>

    <addForeignKeyConstraint
        constraintName="fk_constraint_worker_phone_number"
        referencedTableName="CONTACT_NUMBER" baseColumnNames="ContactNumbers"
        baseTableName="WORKER" referencedColumnNames="id" />

I want 3rd table (like hibernate generate in @OneToMany mapping).

How to do this with liquibase-springboot?

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85

2 Answers2

2

If the relation is truly a OnToMany, you don't need a 3rd table. Simply, add PrimaryKeyJoinColumn.

If the address can be reused for many persons, it's a ManyToMany relation.

You can use @ManytoMany and add information about you joined table un @jointable

Eric
  • 517
  • 2
  • 7
1

Well, in case of liquibase we have to create the 3rd table manually and have to apply the necessary constraints.

Create the table which manages the mapping :


<createTable tableName="PERSON_ADDRESS">
    <column name="PERSON_ID" type="BIGINT">
        <constraints primaryKey="true" nullable="false" />
    </column>
    <column name="ADDRESS_ID" type="BIGINT">
        <constraints primaryKey="true" nullable="false" />
    </column>
</createTable>

Apply the constraints:

1) Ensure that Persons id is unique in the mapping table

2) A foreign key relationship between ADDRESS's id and PERSON_ADDRESS's PERSON_ID

3) A foreign key relationship between PERSON's id and PERSON_ADDRESS's ADDRESS_ID


<addUniqueConstraint
            columnNames="PERSON_ID" tableName="PERSON_ADDRESS"
            constraintName="UK_PHONE_NUMBERS_ID" />

<addForeignKeyConstraint
            constraintName="FK_ADDRESS_PERSON_ADDRESS"
            referencedTableName="ADDRESS"
            baseColumnNames="ADDRESS_ID"
            baseTableName="PERSON_ADDRESS" referencedColumnNames="id" />

<addForeignKeyConstraint
            constraintName="FK_PERSON_PERSON_ADDRESS"
            referencedTableName="PERSON"
            baseColumnNames="PERSON_ID"
            baseTableName="PERSON_ADDRESS" referencedColumnNames="id" />
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85