如何在使用 Hibernate 实现查询时加入 table 的 属性 以检查字符串是否等于它

how to get joined table's property to check if a string is equal to it while implementing a query with Hibernate

我使用的是 Hibernate 5.4.2 版。

我正在使用 CriteriaBuilderCriteriaQuery 生成标准查询以获取一些结果与请求的加入 table 的 "name" 属性 但是我在那个过程中遇到异常。我想检查员工姓名是否等于我提到的字符串。 EmployeeProject 里面的 OneToMany 属性。这是查询:

CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder();
CriteriaQuery<ProjectTable> criteriaQuery = 
criteriaBuilder.createQuery(Project.class);
Root<ProjectTable> projectTableRoot = criteriaQuery.from(Project.class);     
projectTableRoot.alias("employee");

Predicate employeePredicate = criteriaBuilder.like(projectTableRoot.get("employee").get,"%"+ StringUtils.lowerCase(employee.getName()) + "%");

实体:

public class ProjectTable {

    @OneToMany(mappedBy = "projectTable", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Employee> employee= Lists.newArrayList();
}

public class EmployeeTable {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_employee_fk", referencedColumnName = "id")
    @DiffIgnore
    private ProjectTable projectTable;
}

当两个实体:评论和Post,其中评论@ManyToOne Post

CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder();
CriteriaQuery<Comment> criteriaQuery = criteriaBuilder.createQuery(Comment.class);

Root<Comment> root = getRoot(criteriaQuery);

Join<Comment, Post> joinPost = root.join("post");
Predicate predicate = criteriaBuilder.equal(joinPost.get("id"), 2L);

criteriaQuery
        .select(root)
        .where(predicate);

return getSession()
        .createQuery(criteriaQuery)
        .getResultList();