Spring Data Projection 加载额外的字段 [英] Spring Data Projection loads additional Fields

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

问题描述

我有一个小问题.假设我有这个实体:

I have a small issue. Let’s assume I have this Entity:

import lombok.Data;
import javax.persistence.*;
import java.util.Set;

@Data
@Entity
public class Person {

    enum SEX {F, M}

    @Id
    @GeneratedValue
    private Long id;

    @OrderBy
    @Enumerated(EnumType.STRING)
    @ElementCollection(fetch = FetchType.EAGER)
    private Set<SEX> sexes;

    private String firstName;
    private String lastName;
}

因为在某些时候我不想加载名字和姓氏,所以我使用投影(https://docs.spring.io/spring-data/jpa/docs/1.10.2.RELEASE/reference/html/#projections):我定义了这个接口:

Because at some points I do not want to load the first and last name, I use Projections (https://docs.spring.io/spring-data/jpa/docs/1.10.2.RELEASE/reference/html/#projections): I define this Interface:

import java.util.Set;

public interface PersonSlim {

    String getId();
    Set<Person.SEX> getSexes();
}

现在看看这个存储库:

import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface PersonRepo extends CrudRepository<Person, Long> {

    List<PersonSlim> getAllBy();
}

如果您启用 SQL 日志记录 (logging.level.org.hibernate.SQL: DEBUG) 并执行调用 personRepo.getAllBy(); 您将看到:

If you enable the SQL-logging (logging.level.org.hibernate.SQL: DEBUG) and execute the call personRepo.getAllBy(); you will see:

2018-12-14 16:36:36.808 DEBUG 14227 --- [           main] org.hibernate.SQL                        : select person0_.id as id1_0_, person0_.first_name as first_na2_0_, person0_.last_name as last_nam3_0_ from person person0_

所以基本上,Spring 加载所有字段.如果排除 PersonSlim 接口中的 getSexes() 方法,Spring 只会加载 id:

So basically, Spring loads all fields. If you exclude the getSexes() Method in the PersonSlim Interface Spring will only load the id:

2018-12-14 16:38:03.977 DEBUG 14382 --- [           main] org.hibernate.SQL                        : select person0_.id as col_0_0_ from person person0_

Spring 加载所有字段,如果我的投影包含 ElementCollection.

Spring loads all Fields, if my projection contains an ElementCollection.

为什么这是一个问题?
我使用带有几何字段的 PostGIS-DB,并且几何图形可能非常大.因此,如果我加载许多实体,它会变得很慢.在某些情况下,我不想加载此字段.

Why is that a Problem?
I use a PostGIS-DB with geometry Fields and the geometries can be very large. So, if I load many Entities it becomes slow. At some situations I do not want to load this fields.

推荐答案

似乎当投影包含非原始类型时,所有列都会包含在查询中.

It seems that when the projection contains non-primitive types, all columns will be included in the query.

这里已经有问题https://jira.spring.io/browse/DATAJPA-1218,而且从这个问题看来,目前无法通过预测解决这个问题.

There is already an issue here https://jira.spring.io/browse/DATAJPA-1218 , and it sounds from the issue that this can't be solved with projections for now.

这对你很有用 https://github.com/Blazebit/blaze-persistence,记录在此 实体视图

This can be useful for you https://github.com/Blazebit/blaze-persistence , documented here Entity Views

它使用与投影类似的实现.

It uses similar implementation to projections.

这篇关于Spring Data Projection 加载额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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