使用规范和投影的Spring JPA查询

时间:2020-02-25 19:58:46

标签: spring-data-jpa

我使用spring jpa规范来动态构建实体查询。

工作正常,但是查询返回所有实体字段,这使性能变慢。 我只想获取特定的实体字段,而不要获取所有我不想使用且不会使用的实体字段和依赖项。

我在网上搜索,尝试了一些方案,但没有任何不足。 有人可以提出任何解决方案吗?

预先感谢

这里是我所拥有的。我正在使用Spring Boot 2.2.4

> lerna publish from-git --yes --npm-tag beta

WARN deprecated --npm-tag has been renamed --dist-tag

lerna notice cli v3.20.2

lerna info ci enabled

Found 1 package to publish:

 - @my-org/example-pkg => 1.0.2-beta.8

lerna info auto-confirmed 

lerna info publish Publishing packages to npm...

lerna notice Skipping all user and access validation due to third-party registry

lerna notice Make sure you're authenticated properly ¯\_(ツ)_/¯

lerna WARN ENOLICENSE Package @my-org/example-pkg is missing a license.

lerna WARN ENOLICENSE One way to fix this is to add a LICENSE.md file to the root of this repository.

lerna WARN ENOLICENSE See https://choosealicense.com for additional guidance.

lerna WARN lifecycle Skipping root "prepublish" because it has already been called

lerna http fetch PUT 401 https://npm.pkg.github.com/:_authToken=[secure]/@my-org%2fexample-pkg 153ms

lerna ERR! E401 Unable to authenticate, need: Basic realm="GitHub Package Registry"

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! ts-utilities@1.0.0 publish: `lerna publish from-git --yes --npm-tag beta`

npm ERR! Exit status 1

npm ERR! 

npm ERR! Failed at the ts-utilities@1.0.0 publish script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

npm ERR!     /home/travis/.npm/_logs/2020-02-25T19_46_08_862Z-debug.log

Script failed with status 1

failed to deploy

规格:

public class Concert {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String name;

    @Column
    private String code;

    @Column
    private double totalIncome;

    @Column            
    private double totalExpenses;

    @Column
    private double totalBudget;

    @ManyToOne(targetEntity = Orchestra.class, fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "orchestra_id")
    private Orchestra orchestra;

    @ManyToOne(targetEntity = ConcertStatus.class, fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "concert_status_id")
    private ConcertStatus status;

    /* other fields */

}

存储库:

public class ConcertSpecification implements Specification<Concert> {

    @Override
    public Predicate toPredicate(Root<Concert> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        List<Predicate> predicates = new ArrayList<>();

        //add add criteria to predicates
        for (Criterion criteria : criteriaList) {

            /* predicates builder here */
        }

        return builder.and(predicates.toArray(new Predicate[0]));
    }
}

ConcertService:

public interface ConcertDao extends JpaRepository<Concert, Long>, JpaSpecificationExecutor<Concert>, PagingAndSortingRepository<Concert, Long> { }

ConcertServiceImpl:

public interface ConcertService {
    Page<Concert> findAll(@Nullable Specification<Concert> spec, Pageable pageable); 
}   

0 个答案:

没有答案