如何使用其他列创建连接表

时间:2012-05-09 05:05:24

标签: hibernate hibernate-annotations

我有以下用户表和联系人表。我想要做的是创建一个连接表,允许我加入一个用户到3个联系人 - 家庭住址,账单地址和送货地址。

目前我在user和user_address之间有一对一的映射,然后我在user_address和每个不同的地址类型之间进行一对一的映射。

我已经玩了很多额外的列,一对多,但到目前为止还没有成功。

我有没有其他方法可以做到这一点?

一对一映射:

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;

1 个答案:

答案 0 :(得分:0)

没有“与额外列连接表”这样的事情。如果存在非联接相关列,则它本身就是一个实体,它应该被视为这样。您当前的策略是最好的策略。对此感到满意:-)