1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Lolly
  • 34,250
  • 42
  • 115
  • 150
  • How it comes all 3 `pidXXX` columns are part of the composite key but one of them does not participate in foreign key? Are all of these *really* should be part of id? Try make `pid3` and `cid3` standalone properties, not part of embeddable. – Vasily Liaskovsky Oct 07 '21 at 18:53
  • Thats correct pid3 is part of composite key and not a foreign key for child table. You mean I should not make pid3 and cid3 as part of composite Id columns ? – Lolly Oct 07 '21 at 19:19
  • I mean - if a `Parent` entity is identified by 3 columns, how it's possible that 2 columns from `Child` would be enough to define its relation? The problem is nothing to deal with JPA, Hibernate or Spring, it's a relational model flaw. It's either a join column is missing for a relation, or there is surplus one for primary id. And this is basically what Hibernate is saying - *"Hey, your composite id for relation is incomplete!"* – Vasily Liaskovsky Oct 07 '21 at 20:40
  • Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Oct 08 '21 at 08:11

0 Answers0