Trade と Location の 2 つのテーブルがあり、多対多の関係が存在します。中間テーブル trade_location に「ステータス」として余分な列を追加する必要があります。 「id」として独自の一意の主キーとして「trade_location」テーブルとして複合キーを使用しないソリューションが必要です。追加の列を使用して複合キーを使用せずに多対多の関係を実現するための提案。Thanks.Here は私の 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") public class 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
}