2
我有兩個表:貿易和位置,並存在多對多的關係。現在我必須在中間表trade_location添加額外的列作爲「狀態」 。我搜索了很多,但提供的所有解決方案都是使用複合鍵。但我想要一個沒有複合鍵的解決方案作爲「trade_location」表作爲它自己的唯一主鍵作爲「id」。任何建議實現與extra列的manytomany關係和沒有組合鍵。 Thanks.Here是我的POJO:ManyToMany與額外的列的關係,並沒有使用Spring和休眠的複合關鍵概念
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 = 「位置」) 公共類位置{
@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
}
我也一樣,但它不工作作爲貿易也有批量class.It的捐贈以下錯誤一到一個關係:執行加載命令時出錯:org.hibernate.HibernateException:找到多於一行的給定標識符:5 – Suresh
顯示新的源代碼。 – shazin