地址表具有带复合键的以下结构,user_id是外键,也是复合PK的一部分:
address_ordinal(PK) user_id(PK, FK)
1 1
2 1
1 2
2 2
3 2
1 3
Run Code Online (Sandbox Code Playgroud)
地址类别:
@Entity
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private AddressPK id;
}
@Embeddable
public class AddressPK implements Serializable {
@GeneratedValue //what strategy ?
@Column(name="address_ordinal")
private Integer addressOrdinal; // is there any strategy or any other way to auto generate addressOrdinal based on user_id ?
@Column(name="user_id")
private Integer userId;
//equals() and hashCode()
}
@Entity
@Table(name = "user_detail") …Run Code Online (Sandbox Code Playgroud)