Spring Boot JPA join columns with a partial composite key

425 Views Asked by At

I have entity classes like this structure:

Class Parent {
  @EmbededId
  private ParentId id;

  @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="parentDetails")
  private List<Child> childDetails;
  ...
}

@Embeddable
Class ParentId {
  private Integer pid1;
  private Integer pid2;
  private Integer pid3;
  ...
}

Class Child {
  @EmbededId
  private ChildId id;

  @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumns({
      @JoinColumn(name="C_ID1" referencedColumnName="P_ID1")
      @JoinColumn(name="C_ID2" referencedColumnName="P_ID2")
    )}
  private Parent parentDetails;
  ...
}

@Embeddable
Class ChildId {
  private Integer cid1;
  private Integer cid2;
  private date cid3;  // its a totally different field
  ...
}

I don't have any relationship with pid3 and cid3 as they are different. If I go with above design I am getting below error:

org.hibernate.AnnotationException: referencedColumnNames(P_ID1, P_ID2) of Child.parentDetails referencing Parent not mapped to a single property

If I comment pid3 then it works. So does that mean that I can't refer part of composite key as join columns? Is there any solution for it? I can't make changes to tables as they are legacy.

0

There are 0 best solutions below