主从表与复合主键

xiaoxiao2021-02-28  1

主表的主键是从表的一个复合主键

主表

@Entity @Table(name = "user") public class User { @Id @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0) private Long id; @Column(name = "USER_NAME", length = 100) private String userName; @Column(name = "USER_PWD", length = 100) private String userPwd; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "userParamKey.user") private List<UserParam> userParams = new ArrayList<UserParam>(); }

从表

@Entity @Table(name = "user_param") public class UserParam implements Serializable { @EmbeddedId @AttributeOverrides({ @AttributeOverride(name = "userId", column = @Column(name = "USER_ID", nullable = false)), @AttributeOverride(name = "openId", column = @Column(name = "OPEN_ID", nullable = false)) }) private UserParamKey userParamKey; @Column(name = "VALUE") private String value; }

从表复合主键

@Embeddable public class UserParamKey implements Serializable { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "USER_ID", nullable = false) private User user; @Column(name = "NAME", nullable = false) private String name; }

以上

转载请注明原文地址: https://www.6miu.com/read-1750304.html

最新回复(0)