2015-02-10 84 views
0

當通過JPA/Hibernate使用Spring Data REST並通過REST公開彈簧JpaRepository時,我收到JPA Criteria查詢的錯誤。這是一個Spring Boot應用程序。當使用JPA CriteriaQuery和連接時,Spring Data REST拋出錯誤

夫婦相關實體:

@Entity 
public class Appointment { 
... 
... 
@ManyToOne(fetch=FetchType.EAGER) 
@JoinColumn(name="doctor_id", insertable=false, updatable=false) 
private Doctor doctor; 
... 
... 
} 

@Entity 
public class Doctor { 
... 
... 
@Column(name = "doctor_name") 
private String doctorName = ""; 
... 
... 
} 

試圖返回預約匹配的醫生名字的名單,我建爲JPA規範如下:

public Specification<Appointment> getSpecification() { 
    return new Specification<Appointment>() { 

     Join<Appointment, Doctor> doctorJoin; 

     @Override 
     public Predicate toPredicate(Root<Appointment> root, 
       CriteriaQuery<?> query, 
       CriteriaBuilder cb) { 
      Predicate p = cb.conjunction(); 
      ... //other critieria 
      p = addDoctorCriteria(p, cb, root, Doctor_.doctorName, getDoctorName()); 

      return p; 
     } 


     private Predicate addDoctorCriteria(Predicate p, 
       CriteriaBuilder cb, Root<Appointment> root, SingularAttribute<Doctor, String> property, String value) { 

      value = value + '%'; 

      if (doctorJoin == null) { 
       doctorJoin = root.join(Appointment_.doctor); 
      } 

      p = cb.and(p, cb.like(cb.lower(doctorJoin.<String>get(property)), value)); 

      return p; 
     } 
    }; 
} 

這引發以下異常:

org.hibernate.hql.internal.ast.QuerySyntaxException: 
Invalid path: 'generatedAlias1.doctorName' 
[select count(generatedAlias0) from my.package.Appointment as generatedAlias0 
where (1=1) and (lower(generatedAlias1.doctorName) like :param0)]; 

看起來像加入查詢是不正確的 - 沒有'generat from子句中的edAlias1'。似乎在Spring試圖獲得行數來構建分頁信息時發生。其他依賴於約會屬性(即沒有加入)的標準很好。

我在做JPA連接是否正確?有關如何糾正此錯誤的任何建議?

回答

0

這是我如何得到這個工作,如果有人遇到這樣的問題:

Root<Doctor> doctor = query.from(Doctor.class); 
Predicate j1 = cb.equal(root.get(Appointment_.doctor), doctor); 
Predicate c1 = cb.like(cb.lower(doctor.<String>get(Doctor_.doctorName)), getDoctorName() + '%'); 
p = cb.and(p, cb.and(j1, c1)); 
+0

你應該紀念這個公認所以它更容易被發現。 – 2015-03-14 04:31:13