我正在尝试设置我的实体以允许pks。我的数据库包含两个字段,

Dealer_detail_id pk
user_detail_id pk

两者都在对应表中的id上联接。

到目前为止,我已经尝试过了,但没有成功。

@Embeddable
public class DealerUserPk implements Serializable {

    private Integer dealerDetail;
    private Integer userDetail;

DealerUser
@Embeddable
@Table(name = "dealer_user", schema = "account")
public class DealerUser implements Serializable {

    @EmbeddedId
    private DealerUserPk id;

    @Id
    @ManyToOne
    @JoinColumn(name = "dealer_detail_id", referencedColumnName = "id")
    private DealerDetail dealerDetail;

    @Id
    @ManyToOne
    @JoinColumn(name = "user_detail_id", referencedColumnName = "id")
    private UserDetail userDetail;

经销商详情
@Entity
@Table(name = "dealer_detail", schema = "account")
public class DealerDetail implements Serializable {

    @Id
    private Integer id;

用户详细信息
@Entity
@Table(name = "user_detail", schema = "account")
public class UserDetail implements Serializable {

    @Id
    private Integer id;

有人可以发现我在做什么吗?

最佳答案

这是对的:

@Embeddable
public class DealerUserPk implements Serializable {

    private Integer dealerDetail;
    private Integer userDetail;
  • 但是您的DealerUser带有可嵌入的注释,它应该是@Entity
    因为您正在使用@Table批注。
  • 需要如下添加MapsId
     @Entity
     @Table(name = "dealer_user", schema = "account")
      public class DealerUser implements Serializable {
    
    @EmbeddedId
    private DealerUserPk id;
    
    @MapsId("dealerDetail")
    @ManyToOne
    @JoinColumn(name = "dealer_detail_id", referencedColumnName = "id")
    private DealerDetail dealerDetail;
    
    @Id
    @MapsId("userDetail")
    @JoinColumn(name = "user_detail_id", referencedColumnName = "id")
    private UserDetail userDetail;
    

  • 尝试一下。

    关于java - Hibernate JPA ManyToOne复合键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22002739/

    10-12 00:23