次のユーザーテーブルと連絡先テーブルがあります。私がしたいのは、1 人のユーザーを 3 つの連絡先 (自宅住所、請求先住所、配送先住所) に結合できるようにする結合テーブルを作成することです。
現在、user と user_address の間に 1 対 1 のマッピングがあり、次に user_address とそれぞれの異なるアドレス タイプの間に 1 対 1 のマッピングがあります。
私は多対多で余分な列、1対多で遊んでみましたが、これまでのところ成功していません。
私がこれを行った方法に代わるものはありますか?
1 対 1 のマッピング:
User.java
/**
* Relationship to the User Address - extension of User
*/
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user",fetch=FetchType.EAGER)
private UserAddress userAddress;
UserAddress.java
@Entity
@Table(name = "a_user_address")
@GenericGenerator(name = "user-primarykey", strategy = "foreign", parameters = @Parameter(name = "property", value = "user"))
@Audited
public class UserAddress extends Trackable implements Serializable
{
/**
* The unique identifier associated with the user. References user(id).
*/
@Id
@GeneratedValue(generator = "user-primarykey")
@Column(name = "user_id", unique = true, nullable = false)
private Long userId;
/**
*
*/
@OneToOne
@PrimaryKeyJoinColumn
private User user;
/**
* The unique identifier associated with the user's home/profile address
*/
@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="home_addr_id", nullable=true, updatable=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private ContactInfo homeAddress;
/**
* The unique identifier associated with the user's billing address
*/
@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="billing_addr_id", nullable=true, updatable=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private ContactInfo billingAddress;
/**
* The unique identifier associated with the user's shipping address
*/
@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="shipping_addr_id", nullable=true, updatable=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private ContactInfo shippingAddress;