【问题标题】:Many to Many Hibernate mapping with extra column带有额外列的多对多 Hibernate 映射
【发布时间】:2014-10-24 14:44:04
【问题描述】:

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

我正在尝试将多对多关系分解为分离多对一和一对多实体,以便我可以添加其他列。据我所知,主要实体保存到数据库中,但中间实体没有。如果有人能弄清楚发生了什么,我将不胜感激。我尝试使用主键组合(又名:@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,但它不会改变结果。

无论如何我知道它有很多代码要看,但在此先感谢。

【问题讨论】:

    标签: java hibernate many-to-many model-associations


    【解决方案1】:

    原来我在这个问题上解决了我自己的问题。问题是我没有在 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>();
    

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 2011-12-05
      • 2016-05-11
      相关资源
      最近更新 更多