简体   繁体   English

使用 JPA Criteria,如何在不获取已加入实体的情况下获取已加入实体的子实体?

[英]With JPA Criteria, how would I Fetch a child entity of a Joined entity without Fetching the Joined entity?

On my project I'm using Groovy with Spring Data JPA's Specification 's to construct Hibernate queries.在我的项目中,我使用 Groovy 和 Spring Data JPA 的Specification来构建 Hibernate 查询。

I can't provide my actual queries but to illustrate my problem let's say I have Building entities, and each Building has Floors and each Floor has both Rooms and Windows.我无法提供我的实际查询,但为了说明我的问题,假设我有建筑实体,每个建筑都有楼层,每个楼层都有房间和窗户。

The behavior I'm attempting to simulate is something like this native SQL query:我试图模拟的行为类似于本机 SQL 查询:

SELECT b.*, r.*
FROM building b
INNER JOIN floor f ON b.id = f.building_id
INNER JOIN window w ON f.id = w.floor_id
LEFT OUTER JOIN room r ON f.id = r.floor_id
WHERE w.id = 1;

I have a specification similar to the below:我有一个类似于以下的规范:

public class MySpec implements Specification<Building> {
    @Override
    public Predicate toPredicate(final Root<Building> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
        final Join floorsJoin = root.join("floors");
        final Join windowsJoin = floorsJoin.join("windows");

        //I'd like to remove this line
        final Fetch floorsFetch = root.fetch("floors"); // <---

        floorsFetch.fetch("rooms", JoinType.LEFT);

        cb.equal(windowsJoin.get("id"), 1L);
    }
}

The line annotated above is my issue.上面注释的行是我的问题。 If I leave it, the generated query looks something like this:如果我离开它,生成的查询看起来像这样:

SELECT b.*, f2.*, r.*
FROM building b
INNER JOIN floor f ON b.id = f.building_id
INNER JOIN window w ON f.id = w.floor_id
INNER JOIN floor f2 ON b.id = f2.building_id
LEFT OUTER JOIN room r ON f2.id = r.floor_id
WHERE w.id = 1;

(notice the duplicate INNER JOIN of floor and the unneeded f2.* data) (注意floor的重复INNER JOIN和不需要的f2.*数据)

If I remove it, and use the floorsJoin instead to fetch rooms, I get the following Hibernate error:如果我删除它,并使用floorsJoin来获取房间,我会收到以下休眠错误:

org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list

The unneeded f2.* data would be OK except I can't replace the above floorsJoin with the floorsFetch because I need to join with the windows table (without fetching windows ) and the Fetch class doesn't have a .join method.不需要的f2.*数据将是美好的,除了我不能代替上述floorsJoinfloorsFetch ,因为我需要用加入windows表(不取windows )和Fetch类没有一个.join方法。

I'm having a difficult time figuring out how I would accomplish what I need while still generating a single query;我很难弄清楚如何在生成单个查询的同时完成所需的工作; surely I must be missing something simple.我肯定错过了一些简单的东西。

Any thoughts or advice you could provide would be much appreciated.您可以提供的任何想法或建议将不胜感激。

Thanks a lot, BJ非常感谢,BJ

Well it's not that simple with the JPA Criteria API.好吧,使用 JPA Criteria API 并不是那么简单。 With Hibernate you could simply cast the Fetch to a Join I guess but that's not going to help you that much.使用 Hibernate,您可以简单地将Fetch转换为Join我想但这不会对您有多大帮助。 I am not sure how you use the specification in this case, but if you could write the query as a whole it could look like the following我不确定在这种情况下您如何使用规范,但是如果您可以将查询作为一个整体编写,它可能如下所示

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();

Root<Building> root = cq.from(Building.class);
final Join floorsJoin = root.join("floors");
final Join windowsJoin = floorsJoin.join("windows");
final Join roomsJoin = floorsJoin.join("rooms", JoinType.LEFT);

cb.equal(windowsJoin.get("id"), 1L);

cq.multiselect(
    root,
    roomsJoin
);

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

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