Spring Data JPA提取 [英] Spring Data JPA Fetching

查看:122
本文介绍了Spring Data JPA提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以定义一个Spring Data Specification(返回一个JPA谓词),其唯一目的是执行热切取回操作?

Is there a way to define a Spring Data Specification (returning a JPA predicate) whose sole purpose is to perform eager fetching?

我有一个使用延迟加载定义各种关系的实体,但是有几个查询要返回包括所有相关集合在内的整个实体表示,但是这些查询的条件可能有所不同.我看过几篇文章(例如在春季论坛),讨论可能会引入访存组,这可能是理想的解决方案;但是,由于这还不是JPA规范的一部分,因此Spring Data对此不提供支持.

I have an entity which defines various relationships using lazy loading, but there are several queries where I want to return the entire entity representation including all related collections, but the criteria of these queries may vary. I've seen a few posts (e.g. on the spring forum) that discuss the potential introduction of fetch groups, which would likely be the ideal solution; however, since this is not yet part of the JPA spec, Spring Data does not provide support for it.

我正在寻找一种可重用的方法来对各种动态查询执行热切的获取.

I'm looking for a reusable way to perform eager fetching on a variety of dynamic queries.

例如,我考虑过开发一个可重用的规范,其唯一目的是执行急切的加载,并且可以与其他规范结合使用,例如:

For example, I've considered developing a reusable Specification whose sole purpose is to perform the eager loading, and which could be combined with other specifications, e.g:

private static Specification<MyEntity> eager() {

    return new Specification<MyEntity>() {

        @Override
        public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            for (PluralAttribute<? super MyEntity, ?, ?> fetch : root.getModel().getPluralAttributes()) {
                root.fetch(fetch, JoinType.LEFT);
            }
            query.distinct(true);
            return null;
        }

    };
}

此规范的目标是在各种查询中重用它,例如:

The goal of this specification is to reuse it in various queries, e.g:

repository.findAll(where(eager()).and(otherCriteria).or(otherCriteria));

但是,急切的获取规范不是真正的谓词,因此它返回null,并且与其他谓词链接时会引起明显的问题(即NullPointerExceptions).

However, the eager fetching specification is not a true predicate, so it returns null, and would cause obvious problems (i.e. NullPointerExceptions) when chained with other predicates.

(请注意,此谓词单独确实可以正常工作;即以下查询将正确获取:repository.findAll(eager());).

(Note that this predicate alone does work as expected; i.e. the following query will properly fetch: repository.findAll(eager());).

是否存在可以从"eager"规范返回的适当的非null谓词,或者是否有其他可重用的方法使用Spring Data JPA规范来触发eager fetch(而不必将急切的负载添加到另一个规范上) ?

Is there an appropriate non-null Predicate that can be returned from the "eager" specification, or are there any other reusable approaches for triggering eager fetches using Spring Data JPA specifications (without having to tack the eager load onto another specification)?

推荐答案

我们在修复nullSpecification s和Predicate s的处理. .springsource.org/browse/DATAJPA-300"rel =" noreferrer> DATAJPA-300 .您可能想尝试一下1.4快照,看看它如何影响您的场景.

We have improved the handling of null Specifications and Predicates in the course of fixing DATAJPA-300. You might wanna give the 1.4 snapshots a try and see how this affects your scenario.

这篇关于Spring Data JPA提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆