0

I am having composite primary key with 2 columns in one entity (which is parent) and composite primary key with 4 columns in other entity (which is child )i want to create a foreign key with 2 columns of child which is a part of composite primary key with reference to two columns of composite primary key of parent.As the child columns are the part of primary key i cant make it "insertable = false, updatable=false" then i am getting the primary key cannot be null exception.

@Entity
@IdClass (MyKey.class)
@Table (name = "Child_Table")
public class Child {
    @Id
    @XmlElement (name = "C_feild1", required = true)
    protected String C_feild1;
    @Id
    @XmlElement (name = "C_feild2", required = true)
    protected String C_feild2;
    @Id
    @XmlElement (name = "C_feild3", required = true)
    protected String C_feild3;
    @Id
    @XmlElement (name = "C_feild4", required = true)
    protected String C_feild4;

    //getters and setters 
}


@Entity
@IdClass (MyKey2.class)
@Table (name = "Parent_Table")
public  class Parent {
    @Id
    @CascadeOnDelete
    @XmlElement (name = "P_feild1", required = true)
    protected String P_feild1;
    @Id
    @XmlElement (name = "P_feild2", required = true)
    protected String P_feild2;

    //getters and setters 
}

i want to create a relation just like below in between these entities

ALTER TABLE Child_Table ADD CONSTRAINT fk_child_table_parent_table FOREIGN KEY (C_field1 , C_field2) REFERENCES Parent_Table (P_field1 , P_field2) ON DELETE CASCADE;

which should allow to update the table independently and deletes the child row when parent get deleted.

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
  • You can't put EclipseLink's CascadeOnDelete on a basic mapping, as it doesn't tell JPA what other row/entity needs to be removed. You need to setup the relationship between parent and child to be able to use it. https://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/a_cascadeondelete.htm – Chris Mar 28 '17 at 17:24

1 Answers1

1

You need to setup the relationship between your entities if you need JPA to know what needs to be done when you remove the Parent entity.

This would work on something like:

@Embeddable
public class ChildKey {
    protected String C_feild3;
    protected String C_feild4;
    protected MyKey2 parentKey;
}

public class Child {
    @EmbeddedId 
    ChildKey id;

    @MapsId("parentKey")
    @JoinColumns({ 
        @JoinColumn(name="C_feild1", referencedColumnName="P_field1"),
        @JoinColumn(name="C_feild2", referencedColumnName="P_field2")
    })
    protected Parent parent;

    //getters and setters 
}

@Entity
@IdClass (MyKey2.class)
@Table (name = "Parent_Table")
public  class Parent {
    @Id
    protected String P_feild1;
    @Id
    protected String P_feild2;

    @OneToMany(mappedby="parent")
    @CascadeOnDelete
    List<Child> children;

    //getters and setters 
}

This ignores the flat nature you are using for XML, so might not be what you want to deal with.

If you want to keep flat objects, JPA doesn't know anything about the parent-child relationship, but it doesn't really need to either. All you need to do is clean up any child instances that may have been deleted from the shared cache, or just not enable the shared cache for the child class as described here: https://wiki.eclipse.org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F

Chris
  • 20,138
  • 2
  • 29
  • 43