我有这个自我关系注释
public class Estoria implements Serializable {
@EmbeddedId
protected EstoriaPK estoriaPK;
@ManyToOne
@JoinColumns({@JoinColumn(name = "id_estoria", referencedColumnName = "id", updatable = false, insertable = false, nullable = true)
,@JoinColumn(name = "id_projeto" , referencedColumnName = "id_projeto" , insertable = false, updatable = false)})
private Estoria subtask;
@OneToMany(mappedBy = "subtask",cascade = CascadeType.ALL)
private Collection<Estoria> subtasks;
}
我试着坚持这个集合::
public void persistSubtasks(int idEstoria, Collection<Estoria> subtasks) {
Estoria rootEstoria = entityManager.createNamedQuery("Estoria.findById", Estoria.class)
.setParameter("id", idEstoria)
.getSingleResult();
rootEstoria.setSubtasks(subtasks);
}
但是数据库持久性没有持久化根引用ID:
+----+------------+------------+--------------------------+----------- -------------------------------------------------------------------+------ ------+--------+--------------+
| id | id_projeto | id_estoria | nome | descricao | estimativa | status | data_criacao |
+----+------------+------------+--------------------------+------------------------------------------------------------------------------+------------+--------+--------------+
| 1 | 7 | NULL | Check all old activities | No one know if every feature was checked before builded | NULL | 0 | 2015-01-02 |
| 2 | 7 | "supost to be 1" | Register all users | Everyone needs to be registered and identified by profile | NULL | 0 | 2015-01-02 |
有人能给我一些帮助吗?
好消息这篇文章&gt; Understanding annotations and JPA(hibernate)解决了我的问题,但我真的不知道我需要引用root&gt;&gt;叶/叶&gt;&gt;根
答案 0 :(得分:0)
您必须在Child ID中设置父ID。在子任务集合中设置rootEstoria id。
public void persistSubtasks(int idEstoria, Collection<Estoria> subtasks) {
Estoria rootEstoria = entityManager.createNamedQuery("Estoria.findById", Estoria.class)
.setParameter("id", idEstoria)
.getSingleResult();
for(Estoria subtask:subtasks){
subtask.setSubtask(rootEstoria);
}
rootEstoria.setSubtasks(subtasks);
}