我在这里已经看过几次这个问题,但是没有一个答案可以解决我的问题。
我正在尝试解构多对多关系以分离多对一和一对多实体,以便我可以添加其他列。从我所拥有的,主要实体保存到数据库,但中间人没有。如果有人能弄清楚发生了什么,我会非常感激。我尝试使用主键复合(aka:@AssociationOverride)以另一种方式执行此操作,但它也无法正常工作。我已经浏览了网页,但在这里找不到我的问题的答案。
这是我的主要实体,MaintOrder:
@Entity
@Table(name="maint_orders")
public class MaintOrder extends PersistedObject implements java.io.Serializable {
...
@OneToMany(cascade = CascadeType.ALL, mappedBy="maintOrder")
private Set<ManPowerLine> manPower = new HashSet<ManPowerLine>() ;
public void addManPower(ManPower manPower, Integer quantity, Float price) {
ManPowerLine mpLine = new ManPowerLine();
mpLine.setManPower(manPower);
mpLine.setMaintOrder(this);
mpLine.setManPowerID(manPower.getManPowerID());
mpLine.setMaintOrderID(this.getMaintOrderID());
mpLine.setQuantity(quantity);
mpLine.setPrice(price);
this.manPower.add(mpLine);
// Also add the association object to the employee.
manPower.getMaintOrder().add(mpLine);
}
... getters and setters
}
这是我的第二个实体,ManPower:
@Entity
@Table(name="man_power")
public class ManPower extends PersistedObject implements java.io.Serializable {
...id's, etc
@OneToMany(mappedBy="manPower", fetch = FetchType.EAGER)
private Set<ManPowerLine> maintOrder = new HashSet<ManPowerLine>();
public Set<ManPowerLine> getMaintOrder(){
return maintOrder;
}
public void setMaintOrder(Set<ManPowerLine> maintOrder){
this.maintOrder = maintOrder;
}
... other getters and setters
}
这是我的中间实体ManPowerLine:
@Entity
@Table(name = "man_power_line")
@IdClass(ManPowerLineID.class)
public class ManPowerLine extends PersistedObject implements java.io.Serializable {
@Id
private Long maintOrderID;
@Id
private Long manPowerID;
@Column(name="quantity")
private Integer quantity;
@Column(name="price")
private Float price;
@ManyToOne
@JoinColumn(name = "maintOrderID", updatable = false, insertable = false, referencedColumnName = "maint_order_id")
private MaintOrder maintOrder;
@ManyToOne
@JoinColumn(name = "manPowerID", updatable = false, insertable = false, referencedColumnName = "man_power_id")
private ManPower manPower;
... other getters and setters
}
我的ID实体ManPowerLineID:
public class ManPowerLineID implements java.io.Serializable {
private Long maintOrderID;
private Long manPowerID;
public Long getMaintOrderID(){
return maintOrderID;
}
public Long getManPowerID(){
return manPowerID;
}
public void setMaintOrderID(Long maintOrderID){
this.maintOrderID = maintOrderID;
}
public void setManPowerID(Long manPowerID){
this.manPowerID = manPowerID;
}
@Override
public int hashCode(){
return (int)(maintOrderID + manPowerID);
}
@Override
public boolean equals(Object obj) {
if( obj instanceof ManPowerLine){
ManPowerLineID otherID = (ManPowerLineID)obj;
boolean hey = (otherID.maintOrderID == this.maintOrderID) && (otherID.manPowerID == this.manPowerID);
return hey;
}
return false;
}
}
最后利用它的代码如下:
private void insertObject( ) {
ServiceLocator locator = new ServiceLocator();
SessionFactory sf = locator.getHibernateSessionFactory();
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
MaintOrder m = new MaintOrder();
... various setters to m
Set manPowerSet = new HashSet();
for(int i = 0; i < manPowerSet.size(); i++){
ManPower mp = new ManPower();
mp = (ManPower) manPowerSet.iterator().next();
m.addManPower(mp, quantity, cost);
}
sess.saveOrUpdate(m);
tx.commit();
sess.close();
}
我是否有可能需要使用更多只需m.addManPower添加到该行?我已经尝试添加m.setManPowerLine,但它不会改变结果。
无论如何我知道它需要看很多代码,但提前谢谢。
答案 0 :(得分:0)
原来我修复了自己的问题。问题是我没有在 ALL 的正确位置设置cascade = CascadeType.ALL。具体来说:
@OneToMany(mappedBy="manPower", fetch = FetchType.EAGER)
private List<ManPowerLine> maintOrder = new ArrayList<ManPowerLine>();
应该是:
@OneToMany(mappedBy="manPower", cascade = CascadeType.All)
private List<ManPowerLine> maintOrder = new ArrayList<ManPowerLine>();