这些是我的课程:
@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
// ...
}
@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
// ...
}
当我尝试持久化子对象(c)时,父对象(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那里做到这一点。
答案 0 :(得分:0)
如果没有从儿童到父母的反向关系,则不可能自动发生。这是因为没有JPA提供程序在其缓存中具有所有对象图,这意味着很可能JPA提供者根本不知道父对象。
所以解决问题你可以做其中一件事:
EntityManager.persist()
)。