简体   繁体   English

Hibernate一对多连接在两列上

[英]Hibernate One-To-Many join on two columns

I have a SQL query that closely resembles the following: 我有一个与以下内容非常相似的SQL查询:

select *
from parent p inner join child c on p.id = c.parent_id and c.type = 'PARENT_TYPE';

I am trying to define my entity mappings with a @One-To-Many association so that I can load the parent object and it will automatically load the children. 我正在尝试使用@ One-To-Many关联定义我的实体映射,以便我可以加载父对象,并且它将自动加载子对象。 The thing I don't know how to do, is how to properly annotate my entities for this to happen correctly with the second part of the join criteria (ie the c.type = 'PARENT_TYPE' part). 我不知道怎么做,是如何正确地注释我的实体,以便使用连接条件的第二部分(即the c.type = 'PARENT_TYPE'部分)正确地进行此操作。 I tried using a @FilterDef and then enabling the filter on the session and it worked, but it doesn't seem like the optimal solution. 我尝试使用@FilterDef ,然后在会话上启用过滤器,但它确实起作用,但它似乎不是最佳解决方案。

If you use a join-formula, you will lose other abilities (such as sub-select query fetch optimization). 如果使用联接公式,则将失去其他功能(例如子选择查询获取优化)。 Why aren't the two columns you are joining on part of a key. 为什么不是您要在键的一部分上连接的两列。 Introduce an embedded-id type consisting of the two columns and join using that type. 介绍由两列组成的嵌入式ID类型,并使用该类型进行联接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM