I have the following a user table and a contact table. What I would like to do is create a join table which would allow me to join a single user to 3 contact - Home address, Billing Address and Shipping address.
Currently I have a one to one mapping between user and user_address and then I have one to one mapping between user_address and each of the different address types.
I have played around with the many to many with extra columns, one to many but so far no success.
Is there any alternatives to the way I have done this?
One to One Mapping:
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;