我清楚地努力理解這使幫助不勝感激...註解映射雙向OneToMany/ManyToOne不提取?
我有以下映射:
@Entity
@Table(name = "parent")
public class ParentEntity
{
...
@Id
@Column(name = "parent_id")
private Long id;
...
@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
private List<ChildEntity> children;
...
}
@Entity
@Table(name = "child")
public class ChildEntity
{
...
@Id
@Column(name = "child_id")
private Long id;
...
@ManyToOne(fetch = FetchType.EAGER)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "parent_id")
private ParentEntity parent;
...
}
在我的分貝
我有:
parent
------
parent_id: 1
child
------
child_id: 1, parent_id: 1
然而
((Parent) session.get(Parent.class, 1)).getChildren()
返回null。
任何人都可以看到我失蹤的東西嗎?
謝謝,p。
編輯
這似乎是更因此與在該集合不在同一個會話的上下文填充會話狀態做,但集合在下屆會議填充...
考慮以下幾點:
void setupRender()
{
debug("existing not just added", (ParentEntity) session.get(ParentEntity.class, 13L));
ParentEntity parent = new ParentEntity();
session.save(parent);
ChildEntity child = new ChildEntity();
child.setParent(parent);
session.save(child);
debug("new one before commit", parent);
sessionManager.commit();
debug("new one after commit", parent);
debug("new one after re-fetch", (ParentEntity) session.load(ParentEntity.class, parent.getId()));
}
private void debug(String prefix, ParentEntity parent)
{
log.debug(prefix + ": parent id: " + parent.getId() + ", Children is "
+ (parent.getChildren() == null ? "null" : "size:" + parent.getChildren().size()));
}
結果如下輸出:
DEBUG - existing not just added: parent id: 13, Children is size:1
DEBUG - new one before commit: parent id: 23, Children is null
DEBUG - new one after commit: parent id: 23, Children is null
DEBUG - new one after re-fetch: parent id: 23, Children is null
因此,如果是由於會話狀態,並且提交不足以觸發重新獲取,那麼我需要做些什麼才能使映射獲取集合?
再次感謝!
查看Hibernate調試數據時是否獲得任何有用的信息? – Jeremy 2011-05-11 03:08:51
請參閱編輯。 BTW數據庫是postgres,我也在使用tapestry-hibernate模塊的tapestry環境中。 – pstanton 2011-05-11 04:20:26