0

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;

4

1 回答 1

0

没有“带有额外列的连接表”之类的东西。如果有非连接相关的列,它本身就是一个实体,它应该被这样对待。你目前的策略是最好的。对此感到高兴:-)

于 2012-05-09T10:24:23.963 回答