【问题标题】:Hibernate: is it possible to retrieve only children matching a condition?Hibernate:是否可以仅检索匹配条件的子项?
【发布时间】:2020-07-13 17:02:22
【问题描述】:

我有以下一对多:

public class EntityA{
 ...
private List<EntityB> entities;
}

实体 b 所在的位置:

public class EntityB{
     ...
     private String type;
}

现在,我对 entityA 进行查询,例如:

@Query("select a from EntityA a where a.field= :field)

是否有可能拥有整个 A 实体,但只有“类型”等于特定实体的孩子? 我试着做:

@Query("select a from EntityA a where a.field= :field and a.entities.type in ('A', 'B'))

但这会导致零结果,而我的意图是检索带有空列表的实体 A。

【问题讨论】:

标签: hibernate spring-data-jpa


【解决方案1】:

如果关系是双向的,或者你可以让它双向,那么你可以从EntityB端查询:

@Query("select b.entityA from EntityB b where b.type in ('A', 'B')")
public Set<EntityA> aEntities();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    相关资源
    最近更新 更多