使用PageRequest上的模型参考进行排序不起作用

时间:2019-06-26 07:37:33

标签: java spring-boot spring-data-jpa

我正在使用Spring数据PageRequest根据用户过滤器搜索数据。我需要我的排序列与搜索过滤器一起保持动态。

但是stateMaster.countryMaster.description, stateMaster.description, description代码无法正常工作,并引发以下错误。

org.springframework.data.mapping.PropertyReferenceException: No property description, stateMaster found for type CountryMaster! Traversed path: DistrictMaster.stateMaster.countryMaster.

这是我的服务代码。

String sortColumn = filterJson.getString("sortColumn");
if ("default".equalsIgnoreCase(filterJson.getString("sortColumn"))) {
    sortColumn = "stateMaster.countryMaster.description, stateMaster.description, description";
} else {
    sortColumn = "description";
}
String sortOrder = filterJson.getString("sortOrder");
Integer maxResult = Integer.parseInt(hbsService.getGeneralConfigValue("adminmaxallowedlisting"));
PageRequest pageRequest = new PageRequest(pageNo, maxResult, Sort.Direction.valueOf(sortOrder), sortColumn);

Page<DistrictMaster> page = districtRepository.findAll(new Specification<DistrictMaster>() {
    @Override
    public Predicate toPredicate(Root<DistrictMaster> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
        List<Predicate> predicates = new ArrayList<>();

        if (filterJson.optLong("stateId") != 0) {
            predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("stateMaster"), filterJson.optLong("stateId"))));
        }

        if (!filterJson.optString("status").trim().isEmpty() && !filterJson.optString("status").equals("All")) {
            predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("active"), filterJson.optBoolean("status"))));
        }

        if (!filterJson.optString("value").trim().isEmpty()) {
            predicates.add(criteriaBuilder.and(criteriaBuilder.like(criteriaBuilder.lower(root.get("description")), "%" + filterJson.optString("value").toLowerCase() + "%")));
        }

        return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
    }
}, pageRequest);

这是我的模特:

package com.agrisk.data.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "tbldistrictmst")
@SequenceGenerator(name = "districtmstseq", allocationSize = 1, sequenceName = "districtmstseq")
public class DistrictMaster extends MakerCheckerBO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 5461420398663313529L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "districtmstseq")
    @Column(name = "id")
    public Long id;

    @Column(name = "description")
    public String description;

    @Column(name = "code")
    public String code;

    @ManyToOne
    @JoinColumn(name = "stateid")
    private StateMaster stateMaster;

    @Column(name = "active")
    public Boolean active;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "districtid")
    public Set<TehsilMaster> tehsilMaster;

    @Column(name = "villagereferenceid")
    private Long villageReferenceId;

    public Set<TehsilMaster> getTehsilMaster() {
        return tehsilMaster;
    }

    public void setTehsilMaster(Set<TehsilMaster> tehsilMaster) {
        this.tehsilMaster = tehsilMaster;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public StateMaster getStateMaster() {
        return stateMaster;
    }

    public void setStateMaster(StateMaster stateMaster) {
        this.stateMaster = stateMaster;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public Long getVillageReferenceId() {
        return villageReferenceId;
    }

    public void setVillageReferenceId(Long villageReferenceId) {
        this.villageReferenceId = villageReferenceId;
    }

}

0 个答案:

没有答案