3

我有两个表:Trade 和 Location,并且存在多对多关系。现在我必须在中间表 trade_location 中添加额外的列作为“status”。我搜索了很多,但提供的所有解决方案都是复合键。但是我想要一个没有复合键的解决方案作为“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(name="location") 公共类位置 {

@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

}

4

1 回答 1

3

TradeLocation如果您希望它充当具有 id 的独立表,则必须创建一个命名实体。

并将您的关系更改为跟随。

Trade<-> 1..* TradeLocation*..1 <->Location

于 2013-10-01T08:01:46.513 回答