我在这里多次看到这个问题,但是没有一个答案可以解决我的问题。
我正在尝试将多对多关系解构为单独的多对一和一对多实体,以便我可以添加其他列。据我所知,主要实体保存到数据库,但中间实体没有保存到数据库。如果有人能弄清楚发生了什么事,我将非常感激。我尝试使用主键组合(又名:@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,但它不会改变结果。
无论如何,我知道有很多代码需要查看,但提前致谢。
最佳答案
结果我解决了我自己的问题。问题是我没有在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>();
关于java - 具有额外列的多对多 Hibernate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26550306/