我清楚地努力理解这使帮助不胜感激...注解映射双向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