具有传递依赖的 ORM 查询
这是我的数据库架构的一部分:
因此,我使用表格页面、点和项目来生成页面的布局。每个项目还绑定了一个项目数据。我的所有表模型都已创建并定义了它们的关系。我可以通过循环以下查询的结果来生成一个页面:
<cfset variables.page = EntityLoad("Page", {id=arguments.id}, true)>
现在,当我尝试做同样的事情时,问题就出现了,但是仅从单个“idee”加载itemsData,这是一个想法用法语。我想不出用entityLoad来做到这一点的方法,所以我尝试使用HQL:
<cfset variables.page = ORMExecuteQuery("
select p
from Page p
left join p.points po
left join po.items it
left join it.itemsData id
where p.id = :pid and id.idIdee = :iid", {pid=arguments.id, iid=session.user.idee}, true)>
这看起来很接近,但事实并非如此......我有时会得到超过1行等等。
任何ORM大师都知道我如何处理这个问题?
Here's is a part of my database schema:
So I'm using the tables pages,points and items to generate the layout of my page. To each item is also tied an itemData. All my tables models are created and their relationship defined. I can generate a page by looping through the result of the following query:
<cfset variables.page = EntityLoad("Page", {id=arguments.id}, true)>
Now the problem comes when I try to do the same thing, but load only the itemsData from a single "idee" which is an idea in french. I can't think of a way to do this with entityLoad so I tried using HQL:
<cfset variables.page = ORMExecuteQuery("
select p
from Page p
left join p.points po
left join po.items it
left join it.itemsData id
where p.id = :pid and id.idIdee = :iid", {pid=arguments.id, iid=session.user.idee}, true)>
which seems close to it but that's not it ... I sometimes get more than 1 row etc etc.
Any ORM guru knows how I could handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当使用 NH 查询时,你总是得到完整的对象。这意味着,您无法使用查询来过滤 itemsData 的想法(您只能通过想法查找 itemsData,但这些 itemsData 将始终是完整的,并且附加了所有想法)。
在 NH 中,您可以使用过滤器。
支持从 idee 到 itemsData 的链接会容易得多。然后你只需加载Idee(你有它的id),然后你就可以将ItemsData附加到它上面。
您只需将其映射为逆袋即可。
When querying with NH you get always complete objects. It means, that you can't filter the ideas of an itemsData with a query (you just can find itemsData by idea, but these itemsData will always be complete, with all the ideas attached to it).
In NH, you could use filter for this.
It would be much easier to support a link from the idee to the itemsData. Then you just load the Idee (you have its id) and then you have the ItemsData attached to it.
You just need to map it as inverse bag.