如何使用CriteriaBuilder编写此查询?
SELECT a FROM Article a JOIN a.categories c WHERE c.id = :idCategory
idCategory是一个参数。
到目前为止,这是我尝试过的操作,但是它返回null。
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Article> crit = builder.createQuery(Article.class);
Root<Article> root = crit.from(Article.class);
Join<Category, Article> join = root.join("categories");
CriteriaQuery<Category> crit2 = builder.createQuery(Category.class);
Root<Category> root2 = crit2.from(Category.class);
crit.select(join).where(builder.equal(root2.get("id"), category.getId())).distinct(true);
Query<Article> q = session.createQuery(crit);