[英]BasicPathUsageException: Cannot join to attribute of basic type
I am using Java 8 with JPA (Hibernate 5.2.1). 我正在使用Java 8和JPA(Hibernate 5.2.1)。 I have a like clause which works perfectly, until I introduce the like clause to make use the foreign key table values too. 我有一个完美的like子句,直到我引入like子句来使用外键表值。
I am getting the following error: 我收到以下错误:
BasicPathUsageException: Cannot join to attribute of basic type BasicPathUsageException:无法加入基本类型的属性
I think the problem is related to the fact that I have a join table, and I am not sure how to create the LIKE Join using the join table. 我认为这个问题与我有一个连接表的事实有关,我不知道如何使用连接表创建LIKE Join。
Employee - employee_category - category 员工 - employee_category - 类别
model (Employee.java): model(Employee.java):
@ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable
(
name="employee_category",
joinColumns={ @JoinColumn(name="EMP_ID", referencedColumnName="ID") },
inverseJoinColumns={ @JoinColumn(name="CAT_ID", referencedColumnName="ID") }
)
private Set<Category> categories;
model (Category.java): model(Category.java):
@Id
private String id;
JPA JPA
When I introduce the following code, it fails with the error above (I think the problem is with this new piece of code): 当我介绍下面的代码时,它失败并出现上述错误(我认为问题在于这段新代码):
Join<T, Category> category = from.join("id");
predicates.add(criteriaBuilder.like(category.<String>get("name"), "%" + searchQuery + "%"));
This is the entire method: 这是整个方法:
protected List<T> findAllCriteria(String[] orderByAsc, String[] orderByDesc, Class<T> typeParameterClass,
int firstResult, int maxResults, String searchQuery) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(typeParameterClass);
// from
Root<T> from = criteriaQuery.from(typeParameterClass);
criteriaQuery.select(from);
// like
if (searchQuery != null && searchQuery.trim().length() > 0) {
List<Predicate> predicates = new ArrayList<Predicate>();
for (String name : getColumnNames(typeParameterClass)) {
Predicate condition = criteriaBuilder.like(from.<String>get(name), "%" + searchQuery + "%");
predicates.add(condition);
}
Join<T, Category> category = from.join("id");
predicates.add(criteriaBuilder.like(category.<String>get("name"), "%" + searchQuery + "%"));
criteriaQuery.where(criteriaBuilder.or(predicates.toArray(new Predicate[] {})));
}
List<T> results = (List<T>) entityManager.createQuery(criteriaQuery).setFirstResult(firstResult)
.setMaxResults(maxResults).getResultList();
return results;
}
Any help will be appreciated. 任何帮助将不胜感激。
求助:加入category = from.join(“categories”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.