简体   繁体   中英

Relation between composite primary key to composite foreign key which is a part of primary key (EclipseLINK)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM