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.