Unable to store data using Many to Many relationship

Hi,
I have a Product entity which has (1 to Many) relationship to AddOnGroup entity. The AddOnGroup Entity has (Many to Many) relationship with AddOnProduct entity. When i try to add AddOnProduct into AddOnGroup entity. The AddOnProduct entity is not storing in the AddOnGroup association. I have attached the sample project for your reference.

test-app.zip (1.0 MB)

ScreenRecording2024-12-30012601-ezgif.com-video-to-gif-converter

Thanks in advance.

Hi @vimalkumar010

Try to tick the “Owning side” checkbox for the AddOnGroup.addOnProducts attribute. Only owning side of an association saves changes.

See more information in the Data Modeling: Many-to-Many Associations guide.

Regards,
Konstantin

Hi @krivopustov,
image

Thanks for your response. When i change the AddOnGroup.addOnProducts to Owning side. Actually, now it does adds the AddOnProducts into AddOnGroup entity.

But i am getting the following error (AddOnGroup.addOnProducts is the owning side), when i try delete the Product Entity as shown below:

image

Regards,
Vimalkumar

Right, because CASCADE is set for “FK constraint action” for AddOnGroup.product attribute which corresponds to the @OnDeleteInverse(DeletePolicy.CASCADE) annotation:

public class AddOnGroup {
// ...

    @OnDeleteInverse(DeletePolicy.CASCADE)
    @JoinColumn(name = "PRODUCT_ID", nullable = false)
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Product product;

So Studio has generated a foreign key which tries to delete AddOnGroup record when corresponding Product is deleted. But reference from the join table between AddOnGroup and AddOnProduct prevents the deletion, because its FK has no CASCADE rule.

Perhaps you should add CASCADE rules to the join table. You can do it by adding the following Liquibase changelog:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"
        objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">

    <changeSet id="1" author="demo">
        <dropAllForeignKeyConstraints baseTableName="GDKM_ADD_ON_PRODUCT_ADD_ON_GROUP_LINK"/>
    </changeSet>
    <changeSet id="2" author="demo">
        <addForeignKeyConstraint baseColumnNames="ADD_ON_GROUP_ID" baseTableName="GDKM_ADD_ON_PRODUCT_ADD_ON_GROUP_LINK"
                                 constraintName="FK_GDKADDONPROADDONGRO_ON_ADD_ON_GROUP" referencedColumnNames="ID"
                                 referencedTableName="GDKM_ADD_ON_GROUP"
                                 onDelete="CASCADE"/>

        <addForeignKeyConstraint baseColumnNames="ADD_ON_PRODUCT_ID"
                                 baseTableName="GDKM_ADD_ON_PRODUCT_ADD_ON_GROUP_LINK"
                                 constraintName="FK_GDKADDONPROADDONGRO_ON_ADD_ON_PRODUCT" referencedColumnNames="ID"
                                 referencedTableName="GDKM_ADD_ON_PRODUCT"
                                 onDelete="CASCADE"/>
    </changeSet>
</databaseChangeLog>

After executing the changelog, Studio will generate another one that will again drop the FKs and recreate them without onDelete="CASCADE". You should “Remove and Ignore” all these changesets to get rid of them in the future:

Screenshot 2024-12-31 at 16.23.34

Regards,
Konstantin