2012-05-09 17 views
0

我有以下用户表和联系表。我想要做的是创建一个连接表,这将允许我加入3个联系人的单个用户 - 家庭住址,帐单地址和发货地址。如何用附加列创建连接表

目前我有一个用户和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; 

回答

0

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