我遇到与criteriaquery用法相关的问题。 这是我的代码
VetPreferredLocation[] preffLoc = null;
Predicate p1 = null;
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Vet> criteriaQuery = builder.createQuery(Vet.class);
Root<Vet> root = criteriaQuery.from(Vet.class);
if (vet.getVetPreferredLocations() != null) {
preffLoc = vet.getVetPreferredLocations().toArray(new VetPreferredLocation[0]);
if (preffLoc != null && preffLoc[0].getZipcode() != null) {
p1 = builder.like(/*companyRoot.join("products").get("category")*/
root.get("vetPreferredLocations").get("zipcode"), preffLoc[0].getZipcode());
}
}
if (p1 != null) {
criteriaQuery.multiselect(root.get("qualification"),
builder.concat(root.get("user").get("firstName") + " ", root.get("user").get("lastName")),
root.get("hospitals").get("name"),
builder.avg(root.get("ratings").get("rating"))).where(p1);
}
List<Vet> vets = null;
try {
vets = entityManager.createQuery(criteriaQuery).getResultList();
} catch (Exception e) {
}
return vets;
我在这里检索基于过滤器的实体。为此,我创建了一个Predicate
对象。
这是实体
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany//(mappedBy = "vet")
private Set<VetPreferredLocation> vetPreferredLocations = new HashSet<>();
public Set<VetPreferredLocation> getVetPreferredLocations() {
return vetPreferredLocations;
}
public void setVetPreferredLocations(Set<VetPreferredLocation> vetPreferredLocations) {
this.vetPreferredLocations = vetPreferredLocations;
}
**未添加完整的成员集,getter setter,constructers等 这是已加入的实体
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "zipcode")
private String zipcode;
@Column(name = "locality")
private String locality;
@Column(name = "longitude")
private Float longitude;
@Column(name = "lattitude")
private Float lattitude;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vet_id")
private Vet vet;
**适当的吸气剂设定者等
执行root.get("vetPreferredLocations").get("zipcode")
时,我收到以下异常
Caused by: java.lang.IllegalStateException: Illegal attempt to dereference path source [null.vetPreferredLocations] of basic type
如果我将root.get()更改为root.join(),则会发生异常,但生成的查询具有错误的表名,即vet_preferred_location
而不是vet_vet_preferred_location
,我看到git tag -l | xargs git tag -d && git fetch -t
。这给出了一个未找到的表异常..
请发表你的建议,如果有的话......