我的问题是,向子项添加父实体的集合并不会在拥有方设置外键。任何人都可以给我提示吗?我在这里做错了什么?
// Entity Class: Paket
@OneToMany(mappedBy = "paket", cascade = CascadeType.ALL, targetEntity = Komponente.class, fetch = FetchType.EAGER)
private List<Komponente> komponenten;
public void setKomponenten(List<Komponente> komponenten) {
this.komponenten = komponenten;
}
public List<Komponente> getKomponenten() {
return komponenten;
}
// Entity Class: Komponente
@ManyToOne()
private Paket paket;
// Using EJB -> Excerpt from abstract service class
@Transactional
public T edit(T entity) throws OptimisticLockException {
try {
T myEntity = getEntityManager().merge(entity);
getEntityManager().flush();
return myEntity;
} catch (OptimisticLockException ex) {
throw ex;
}
//System.out.println("Entity Merged: " + entity);
}
// Performing merge
Paket p = this.paketFacade.find(new Long(1));
p.setKomponenten(komponenteInSelectedPaketList);
this.selectedPaket = this.paketFacade.edit(p);