遍历四个级别的多对一关系的 ejbql 查询的正确语法是什么
我在 JPA(休眠)中有一个简单的数据模型,由多对一和一对一的关系组成,类似于:
town ->状态->州长->民族-> 城镇与州是多对一的大陆
,州与州长是一对一,州长与国家是多对一,国家与大陆是多对一。
我想通过其唯一的 ID 获取单个城镇实例,并急切地使用 ejbql 获取其相关的州、州长、国家和大陆。我相信正确的 ejbql 是:
select t from town t
join fetch t.state s
join fetch s.governor g
join fetch g.nation n
join fetch n.continent c
where t.id=?id
Hiberante 从这个 ejbql 生成正确的 sql,但是当我执行 myTown.getState().getGovernor() 时。我为我的州长取回一个空对象。为什么 hibernate 不填充州长?看起来它不想填充树上超过一级的对象。有人看到我做错了什么吗?
I have a simple data model in JPA (hibernate) consisting of many to one and one to one relationships similar to this:
town -> state -> governor -> nation -> continent
where town is many to one to state, state is one to one to governor, governor is many to one to nation and nation is many to one to continent.
I would like to fetch a single town instance by its unique Id and also eagerly fetch its related state, governor, nation and continent using ejbql. I believe the proper ejbql is:
select t from town t
join fetch t.state s
join fetch s.governor g
join fetch g.nation n
join fetch n.continent c
where t.id=?id
Hiberante generates the correct sql from this ejbql, but when I do myTown.getState().getGovernor(). I get back a null object for my governor. Why is hibernate not populating the governor? It seems like it doesn't want to populate objects more than one level up the tree. Anybody see what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系。我上面的语法确实有效。我的问题不是从以前的某些活动中刷新休眠会话并获取未正确填充的对象的缓存版本。纠正该问题后,一切都很好。
Nevermind. My syntax above does in fact work. My problem was not flushing the hibernate session from some previous activity and getting a cached version of the object that wasn't propertly populated. After correcting that problem, everything is fine.