我在Hibernate中成功建立了一個@ManyToMany
關係,併成功添加了一列,如下所示。與額外列查詢的多對多關係休眠
Activity.class
@Entity
@Table(name = "Activity")
public class Activity {
@Id
@GeneratedValue
private int actId;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.activity",
cascade = { CascadeType.ALL, CascadeType.REMOVE })
private Set<ActivityRepairMap> activityRepairMaps = new HashSet<ActivityRepairMap>();
@NotEmpty
private String actTurno;
@NotEmpty
private String actTexto;
private String actFhc;
public Activity() {
}
// Getters and Setters
}
Repair.class
@Entity
@Table(name = "Repair2")
public class Repair {
@Id
@GeneratedValue
private int repId;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.repair")
private Set<ActivityRepairMap> activityRepairMaps = new HashSet<ActivityRepairMap>();
@NotEmpty(message=Constants.EMPTY_FIELD)
private String repNombre;
private Integer repCant;
public Repair() {
}
// Getters and Setters
}
ActivityRepairMap.class
@Entity
@Table(name="ActivityRepairMap")
@AssociationOverrides({
@AssociationOverride(name="pk.activity", [email protected](name="actId")),
@AssociationOverride(name="pk.repair", [email protected](name="repId"))
})
public class ActivityRepairMap {
private ActivityRepairId pk = new ActivityRepairId();
private Integer actRepCant;
@EmbeddedId
public ActivityRepairId getPk() {
return pk;
}
public void setPk(ActivityRepairId pk) {
this.pk = pk;
}
@Transient
public Activity getActivity() {
return getPk().getActivity();
}
public void setActivity(Activity activity) {
getPk().setActivity(activity);
}
@Transient
public Repair getRepair() {
return getPk().getRepair();
}
public void setRepair(Repair repair) {
getPk().setRepair(repair);
}
@Column(name="actRepCant")
public Integer getActRepCant() {
return actRepCant;
}
public void setActRepCant(Integer actRepCant) {
this.actRepCant = actRepCant;
}
public ActivityRepairMap(){
}
// hashCode and equals methods
}
ActivityRepairId
@Embeddable
public class ActivityRepairId implements Serializable{
private static final long serialVersionUID = -776429030880521951L;
private Activity activity;
private Repair repair;
@ManyToOne
public Activity getActivity() {
return activity;
}
public void setActivity(Activity activity) {
this.activity = activity;
}
@ManyToOne
public Repair getRepair() {
return repair;
}
public void setRepair(Repair repair) {
this.repair = repair;
}
// hashCode and equals method
}
我的問題是我無法查詢所有的維修特定活動使用。 我已經在MySQL Workbench中檢查過存儲在數據庫中的數據是否正確。 如果有人能夠使用HQL或Criteria解釋我,我將不勝感激,我該如何實現這一點。
非常感謝。