带有额外列的多对多Hibernate映射



我在这里见过这个问题好几次了,但没有一个答案能解决我的问题。

我试图解构一个多对多的关系,将多对一和一对多的实体分开,这样我就可以添加额外的列。据我所知,主实体保存到数据库中,但中间实体没有。如果有人能弄清楚发生了什么,我会非常感激。我试着用主键组合(又名:@AssociationOverride)的另一种方式来做这件事,但也没有成功。我浏览了网络,但在这里找不到我的问题的答案。

这是我的主要实体,维护订单:

@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>();

最新更新