这些是我的课程:
@Entity
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private long id;
@OneToMany(orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name="parent_id", nullable = false)
private List<Children> childrens = new ArrayList<Children>();
// ...
}
@Entity
public class Children implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private long id;
// NO BIDIRECTIONAL MAPPING
// ...
}
当我尝试持久保存Children对象(c)时,Parent对象(p)未持久保存在数据库中:
Children c = new Children();
Parent p = new Parent();
p.getChildrens().add(c);
childrenDAO.save(c); // Common DAO implementation. It executes a persist on a entity manager
我怎样才能做到这一点?我需要从ChildrenDAO做到这一点。
如果没有从儿童到父母的反向关系,就不可能自动发生这种情况。这是因为no JPA提供程序在其缓存中没有所有对象图,这意味着JPA提供程序很可能不了解父对象。
因此,解决问题可以执行以下操作之一:
EntityManager.persist()
)。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句