简体   繁体   English

BasicPathUsageException:无法加入基本类型的属性

[英]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.

相关问题 hibernate.jpa.criteria.BasicPathUsageException:无法加入基本类型的属性 - hibernate.jpa.criteria.BasicPathUsageException: Cannot join to attribute of basic type JPA LOJ(左外部联接):无法联接到基本类型的属性 - JPA LOJ (left outer join) : Cannot join to attribute of basic type Hibernate 连接提取问题“无法连接到基本类型的属性” - Hibernate problems with join fetch “Cannot join to attribute of basic type” “基本”属性类型不应为“优先级” - 'basic' attribute type should not be Priority 无法在 JPA @Entity 类中声明 List 属性。 它说“基本”属性类型不应该是容器 - Cannot declare List property in the JPA @Entity class. It says 'Basic' attribute type should not be a container 基本属性类型不应为IDao-intellij消息 - Basic attribute type should not be IDao - intellij message 基本”属性类型不应为“持久性实体” - Basic' attribute type should not be 'Persistence Entity' “基本”属性类型不应是持久性实体 - 'Basic' attribute type should not be Persistence Entity Eclipse无法解析抽象类类型的复合组件属性 - Eclipse cannot resolve composite component attribute of abstract class type 使用 LocalDateTime 的“AttributeConverter 和显式类型不能应用于同一属性” - 'AttributeConverter and explicit Type cannot be applied to same attribute' using LocalDateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM