规范不适用于CriteriaBuilder.and()

时间:2018-09-26 18:09:49

标签: java spring hibernate spring-data-jpa criteria-api

我具有以下数据库结构Database

我正在使用Spring Boot 2.0.5

所有检查的单个瞳孔度量都有许多calc_values。 该calc_values的行类型为"102",值为("-1","0","1","2")

我需要找到所有calc_value类型为"102"等于"2"的考试

因此,在纯SQL中,我执行了以下查询,并且效果很好

select e from examination e
         join pupil_measure m2 on e.id = m2.examination
         join calc_value v on m2.id = v.measure
    where v.type=102 and v.value=2

但是问题是将CrudRepository方法findAll()与JpaSpecifications一起使用

public interface ExaminationRepository 
       extends CrudRepository<Examination,UUID>, 
               JpaSpecificationExecutor<Examination> {}

我有以下规范

public static Specification<Examination> resEq(final String exp){
    return (r,cq,cb)-> cb.and(    
cb.equal(r.join("pupilMeasuresById").join("calcValues").get("type"),102),
cb.equal(r.join("pupilMeasuresById").join("calcValues").get("value"),2D));
}

当我这样做的时候

examinationRepository.findAll(resEq())

我正在获取包含值为1和0的考试的考试列表

我做错了什么?

考试实体

@Entity
public class Examination {
    private UUID id;
    private Timestamp ts;
    private Integer status;
    private Integer number;
    private String comment;
    private Boolean issynthetic;
    @JsonManagedReference
    private Collection<AlcoMeasure> alcoMeasuresById;
    @JsonBackReference
    private Collection<BinaryPath> binaryPathsById;
    @JsonManagedReference
    private Patient patientByPatient;
    @JsonManagedReference
    private Terminal terminalByTerminal;
    @JsonBackReference
    private Collection<OxygenMeasure> oxygenMeasuresById;
    @JsonBackReference
    private Collection<PressureMeasure> pressureMeasuresById;
    @JsonManagedReference
    private Collection<PupilMeasure> pupilMeasuresById;
    @JsonBackReference
    private Collection<WeightMeasure> weightMeasuresById;
    @JsonBackReference
    private Set<EGroup> groups=new HashSet<>();
    @JsonBackReference
    private Set<Answer> answers=new HashSet<>();

    @Id
    @Column(name = "id")
    public UUID getId() {
        return id;
    }

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

    @Basic
    @Column(name = "ts")
    public Timestamp getTs() {
        return ts;
    }

    public void setTs(Timestamp ts) {
        this.ts = ts;
    }

    @Basic
    @Column(name = "status")
    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Basic
    @Column(name = "number")
    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    @Basic
    @Column(name = "comment")
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @Basic
    @Column(name = "issynthetic")
    public Boolean getIssynthetic() {
        return issynthetic;
    }

    public void setIssynthetic(Boolean issynthetic) {
        this.issynthetic = issynthetic;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Examination that = (Examination) o;
        return Objects.equals(id, that.id) &&
                Objects.equals(ts, that.ts) &&
                Objects.equals(status, that.status) &&
                Objects.equals(number, that.number) &&
                Objects.equals(comment, that.comment) &&
                Objects.equals(issynthetic, that.issynthetic);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, ts, status, number, comment, issynthetic);
    }

    @OneToMany(mappedBy = "examination")
    public Collection<AlcoMeasure> getAlcoMeasuresById() {
        return alcoMeasuresById;
    }

    public void setAlcoMeasuresById(Collection<AlcoMeasure> 
alcoMeasuresById) {
        this.alcoMeasuresById = alcoMeasuresById;
    }

    @OneToMany(mappedBy = "examination")
    public Collection<BinaryPath> getBinaryPathsById() {
        return binaryPathsById;
    }

    public void setBinaryPathsById(Collection<BinaryPath> binaryPathsById) {
        this.binaryPathsById = binaryPathsById;
    }

    @ManyToOne
    @JoinColumn(name = "patient", referencedColumnName = "id", nullable = 
false)
    public Patient getPatientByPatient() {
        return patientByPatient;
    }

    public void setPatientByPatient(Patient patientByPatient) {
        this.patientByPatient = patientByPatient;
    }

    @ManyToOne
    @JoinColumn(name = "terminal", referencedColumnName = "id")
    public Terminal getTerminalByTerminal() {
        return terminalByTerminal;
    }

    public void setTerminalByTerminal(Terminal terminalByTerminal) {
        this.terminalByTerminal = terminalByTerminal;
    }


    @OneToMany(mappedBy = "examination")
    public Collection<OxygenMeasure> getOxygenMeasuresById() {
        return oxygenMeasuresById;
    }

    public void setOxygenMeasuresById(Collection<OxygenMeasure> 
oxygenMeasuresById) {
        this.oxygenMeasuresById = oxygenMeasuresById;
    }

    @OneToMany(mappedBy = "examination")
    public Collection<PressureMeasure> getPressureMeasuresById() {
        return pressureMeasuresById;
    }

    public void setPressureMeasuresById(Collection<PressureMeasure> 
pressureMeasuresById) {
        this.pressureMeasuresById = pressureMeasuresById;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "examination")
    @JsonIgnore
    public Collection<PupilMeasure> getPupilMeasuresById() {
        return pupilMeasuresById;
    }

    public void setPupilMeasuresById(Collection<PupilMeasure> 
pupilMeasuresById) {
        this.pupilMeasuresById = pupilMeasuresById;
    }

    @OneToMany(mappedBy = "examination")
    public Collection<WeightMeasure> getWeightMeasuresById() {
        return weightMeasuresById;
    }

    public void setWeightMeasuresById(Collection<WeightMeasure> 
weightMeasuresById) {
        this.weightMeasuresById = weightMeasuresById;
    }

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(
            name="examination_answers",
            joinColumns = {@JoinColumn(name="examination")},
            inverseJoinColumns = {@JoinColumn(name = "answer")}
    )
    public Set<Answer> getAnswers() {
        return answers;
    }

    public void setAnswers(Set<Answer> answers) {
        this.answers = answers;
    }

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(
            name="examination_group",
            joinColumns = {@JoinColumn(name="examination")},
            inverseJoinColumns = {@JoinColumn(name = "group")}
    )

    public Set<EGroup> getGroups() {
        return groups;
    }

    public void setGroups(Set<EGroup> groups) {
        this.groups = groups;
    }


}

pupil_measure实体

@Entity
@Table(name = "pupil_measure", schema = "public", catalog = "newmed")
public class PupilMeasure {
    private UUID id;
    private Timestamp ts;
    private Boolean isFail;
    private Integer flags;
    @JsonManagedReference
    private Collection<CalcValue> calcValues;
    @JsonBackReference
    private Examination examination;

    @Id
    @Column(name = "id")
    public UUID getId() {
        return id;
    }

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

    @Basic
    @Column(name = "ts")
    public Timestamp getTs() {
        return ts;
    }

    public void setTs(Timestamp ts) {
        this.ts = ts;
    }

    @Basic
    @Column(name = "is_fail")
    public Boolean getFail() {
        return isFail;
    }

    public void setFail(Boolean fail) {
        isFail = fail;
    }

    @Basic
    @Column(name = "flags")
    public Integer getFlags() {
        return flags;
    }

    public void setFlags(Integer flags) {
        this.flags = flags;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PupilMeasure that = (PupilMeasure) o;
        return Objects.equals(id, that.id) &&
                Objects.equals(ts, that.ts) &&
                Objects.equals(isFail, that.isFail) &&
                Objects.equals(flags, that.flags);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, ts, isFail, flags);
    }

    @OneToMany(mappedBy = "pupilMeasure")
    public Collection<CalcValue> getCalcValues() {
        return calcValues;
    }

    public void setCalcValues(Collection<CalcValue> calcValues) {
        this.calcValues = calcValues;
    }

    @ManyToOne
    @JoinColumn(name = "examination", referencedColumnName = "id", nullable 
= false)
    public Examination getExamination() {
        return examination;
    }

    public void setExamination(Examination examination) {
        this.examination = examination;
    }
}

calc_value实体

@Entity
@Table(name = "calc_value", schema = "public", catalog = "newmed")
public class CalcValue {
    private UUID id;
    private Double time;
    private Double value;
    private Boolean l;
    private Integer type;
    @JsonBackReference
    private PupilMeasure pupilMeasure;

    @Id
    @Column(name = "id")
    public UUID getId() {
        return id;
    }

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

    @Basic
    @Column(name = "time")
    public Double getTime() {
        return time;
    }

    public void setTime(Double time) {
        this.time = time;
    }

    @Basic
    @Column(name = "value")
    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

    @Basic
    @Column(name = "l")
    public Boolean getL() {
        return l;
    }

    public void setL(Boolean l) {
        this.l = l;
    }

    @Basic
    @Column(name = "type")
    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CalcValue calcValue = (CalcValue) o;
        return Objects.equals(id, calcValue.id) &&
                Objects.equals(time, calcValue.time) &&
                Objects.equals(value, calcValue.value) &&
                Objects.equals(l, calcValue.l) &&
                Objects.equals(type, calcValue.type);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, time, value, l, type);
    }

    @ManyToOne
    @JoinColumn(name = "measure", referencedColumnName = "id", nullable = false)
    public PupilMeasure getPupilMeasure() {
        return pupilMeasure;
    }

    public void setPupilMeasure(PupilMeasure pupilMeasure) {
        this.pupilMeasure = pupilMeasure;
    }
}

0 个答案:

没有答案