ManyToMany与额外列的关系,没有使用Spring和Hibernate的复合键概念

时间:2013-10-01 07:55:26

标签: spring hibernate

我有两个表:交易和位置,并且存在多对多关系。现在我必须在中间表trade_location中添加额外的列作为“状态”。我已经搜索了很多但是提供的所有解决方案都是复合的但是我想要一个没有复合键的解决方案作为“trade_location”表作为它自己唯一的主键作为“id”。任何建议来实现与额外列和没有复合键的多种关系。 谢谢。这是我的pojo:

    enter code here
          @Entity
          @Table(name="trade")
         public class Trade {
     @Id
     @GeneratedValue
     private Long id;
         @Column(unique=true)
     private String name;

     @OneToOne
     @JoinColumn(name="sectorId")
     private Sector sector;

    @ManyToMany(mappedBy = "trades",fetch=FetchType.EAGER)
    private Set<Location> location =new HashSet<Location>();
    public Long getId() {
     return id;
    }

   @ManyToMany(mappedBy = "trades")
   private Set<Trainee> trainee = new HashSet<Trainee>();
       //getter and setter here
    }

    @Entity
    @Table(name="location")
    public class Location implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Long id;


    private String name;
    private Long stateId;
    private Long districtId;
    private Long cityId;
    private Long zoneId;
    @Column(name="yearEstablishment")
    private String yearOfEstablishment;

    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name = "trade_location",
            joinColumns={@JoinColumn(name="locationId")},
            inverseJoinColumns={@JoinColumn(name="tradeId")})
    private Set<Trade> trades = new HashSet<Trade>();
        //getter and setter
 }

//编辑了一个

@Entity
@Table(name="trade")
public class Trade {

@Id
@GeneratedValue
private Long id;
@Column(unique=true)
private String name;

@OneToOne
@JoinColumn(name="sectorId")
private Sector sector;

/*@ManyToMany(mappedBy = "trades",fetch=FetchType.EAGER)
private Set<Location> location =new HashSet<Location>();*/

@OneToMany(mappedBy="trade")
private Set<TradeLocation> tradeLocation = new HashSet<TradeLocation>(0);

@ManyToMany(mappedBy = "trades")
private Set<Trainee> trainee = new HashSet<Trainee>();

@OneToMany
@JoinColumn(name="tradeId")
private Set<Batch>batches =new HashSet<Batch>();
//getter and setter

}

@Entity

@Table(名称= “位置”)    公共课位置{

@Id
@GeneratedValue
private Long id;


private String name;
private Long stateId;
private Long districtId;
private Long cityId;
private Long zoneId;
@Column(name="yearEstablishment")
private String yearOfEstablishment;

/*@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name = "trade_location",
            joinColumns={@JoinColumn(name="locationId")},
            inverseJoinColumns={@JoinColumn(name="tradeId")})
private Set<Trade> trades = new HashSet<Trade>();*/

@OneToMany(mappedBy="location")
private Set<TradeLocation>tradeLocation=new HashSet<TradeLocation>(0);

/*getter and setter*/

}

@Entity
@Table(name = "trade_location")
public class TradeLocation implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="tradeId")
private Trade trade;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="locationId")
private Location location;

//getter and setter

}

1 个答案:

答案 0 :(得分:3)

如果您希望将其作为具有ID的独立表,则必须创建名为TradeLocation的实体。

将你的关系改为跟随。

Trade&lt; - &gt; 1 .. * TradeLocation * .. 1&lt; - &gt; Location