2021年4月1日星期四

JPA Hibernate Primary and foreign key at the same time with composite PK

Situation JPA Hibernate : — I have table1 with composite pk :
t1id1 + t1id2 — I have table 2 with pk : t2id With t2id = t1id1

How can I map this please ? I have tried this :

——

@Entity  public class Table1 {  @EmbeddedId  private Table1pk table1pk;      @MapsId("t1id1")  @ManyToOne  @JoinColumn(name = "shared_id")  private Table2 table2;    }    @Embeddable  public class Table1pk {    @Column(name = "shared_id")  private int t1id1;  private String t1id2;  }    @Entity  public class Table2 {  @Id  @GeneratedValue  @Column(name = "shared_id")  private int t2id;    @OneToMany  List<Table1> table1rows;    }  

I also tried this:

Removed @Embeddable and @EmbeddedId And replace it with :

@Entity  @IdClass(Table1pk.class)   public class Table1 {    @Id  private String t1id2;    @Id  @ManyToOne  @JoinColumn(name = "shared_id")  private Table2 table2;  }  
https://stackoverflow.com/questions/66913302/jpa-hibernate-primary-and-foreign-key-at-the-same-time-with-composite-pk April 02, 2021 at 09:06AM

没有评论:

发表评论