QueryDSL - HHH000104:使用collection fetch指定的firstResult / maxResults;在记忆中应用

时间:2016-03-19 16:55:11

标签: spring jpa spring-data spring-data-jpa querydsl

在我的Spring Boot / Data / JPA / QueryDSL应用程序中,我有以下实体:

@Entity
@NamedEntityGraph(name = "graph.User", attributeNodes = { @NamedAttributeNode("authorities") })
@Table(name = "users")
public class User extends BaseEntity implements UserDetails {

    private static final long serialVersionUID = 8884184875433252086L;

    @Id
    @SequenceGenerator(name = "users_id_seq", sequenceName = "users_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "users_id_seq")
    private Long id;

    private String username;

....

    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "users_authorities", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "authority_id") })
    private Set<Authority> authorities = new HashSet<Authority>();

....

}

@Entity
@Table(name = "authorities")
public class Authority implements GrantedAuthority {

    private static final long serialVersionUID = 6118293571787931020L;

    @Id
    @SequenceGenerator(name = "authorities_id_seq", sequenceName = "authorities_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "authorities_id_seq")
    private Integer id;

    @NotEmpty
    private String name;

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "authorities")
    private Set<User> users = new HashSet<User>();

....

}

以及以下load方法:

    @Override
    public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        PageRequest pageRequest = null;
        Page<User> page;
        int pageIndex = first / pageSize;
        if (sortField == null || sortField.isEmpty()) {
            pageRequest = new PageRequest(pageIndex, pageSize);
        } else {
            Sort.Direction direction = sortOrder == SortOrder.ASCENDING ? Sort.Direction.ASC : Sort.Direction.DESC;
            pageRequest = new PageRequest(pageIndex, pageSize, new Sort(direction, sortField));
        }
        Predicate predicate = UserExpressions.lazyPredicate(filters);

        page = userService.findAll(predicate, pageRequest);

        ....

    }

在此方法调用之后,我看到以下调试信息:

Hibernate: select count(user0_.id) as col_0_0_ from users user0_ where user0_.id is not null
1266079 WARN  o.h.h.i.ast.QueryTranslatorImpl - HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
Hibernate: select user0_.id as id1_10_0_, authority2_.id as id1_0_1_, user0_.created_date as created_2_10_0_, user0_.updated_date as updated_3_10_0_, user0_.account_non_locked as account_4_10_0_, user0_.age as age5_10_0_, user0_.birthday as birthday6_10_0_, user0_.email as email7_10_0_, user0_.enabled as enabled8_10_0_, user0_.first_name as first_na9_10_0_, user0_.gender as gender10_10_0_, user0_.last_name as last_na11_10_0_, user0_.password as passwor12_10_0_, user0_.username as usernam13_10_0_, authority2_.name as name2_0_1_, authoritie1_.user_id as user_id1_10_0__, authoritie1_.authority_id as authorit2_11_0__ from users user0_ left outer join users_authorities authoritie1_ on user0_.id=authoritie1_.user_id left outer join authorities authority2_ on authoritie1_.authority_id=authority2_.id where user0_.id is not null

正如您所看到的,根据"WARN o.h.h.i.ast.QueryTranslatorImpl - HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!" - 分页应用于内存中,在数千条记录的情况下,这种分页是不完全可以接受的。

如何更改我的配置以便在数据库级别应用分页?

0 个答案:

没有答案