遍历四个级别的多对一关系的 ejbql 查询的正确语法是什么

发布于 2024-08-13 00:27:49 字数 513 浏览 7 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2024-08-20 00:27:49

没关系。我上面的语法确实有效。我的问题不是从以前的某些活动中刷新休眠会话并获取未正确填充的对象的缓存版本。纠正该问题后,一切都很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文