元组结果 Criteria API 子查询
我正在尝试在使用 JPA 2.0 类型安全标准 API 编写的应用程序中使用子查询,并使用 Hibernate 3.6.1.Final 作为我的提供程序。我选择基本类型(Long、MyEntity 等)没有问题,但我想选择多个列。
这是一个完全合理的例子。忽略子查询的不必要使用——它只是为了说明。
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Subquery<Tuple> subQ = cq.subquery(Tuple.class);
Expression<Long> subqCount;
{
Root<MyEntity> root = subQ.from(MyEntity.class);
Path<MyEntity> filter = root.get(MyEntity.challenge);
subqCount = cb.count(root);
// How to select tuple?
Selection<Tuple> tuple = cb.tuple(filter, subqCount);
// !! Run-time exception here
Expression<Tuple> tupleExpr = (Expression<Tuple>) tuple;
// Not sure why I can't use multiSelect on a subQuery
// #select only accepts Expression<Tuple>
createSubQ.select(tupleExpr);
createSubQ.groupBy(filter);
}
cq.multiselect(subqCount);
尽管编译器没有抱怨,但我仍然遇到运行时异常。
java.lang.ClassCastException: org.hibernate.ejb.criteria.expression.CompoundSelectionImpl cannot be cast to javax.persistence.criteria.Expression
- 这是休眠中的错误,还是我做错了什么?
- 如果您无法在子查询上使用
multiselect
,那么如何执行groupBy
? - 如果您不能在子查询上使用
groupBy
,为什么它会出现在 API 中?
I am trying to use subqueries in an application I am writing using JPA 2.0 type-safe criteria API, with Hibernate 3.6.1.Final as my provider. I have no problem selecting primitive types (Long, MyEntity, etc.), but I want to select multiple columns.
Here's an example of something completely reasonable. Ignore the needless use of subquery -- it is simply meant as illustrative.
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Subquery<Tuple> subQ = cq.subquery(Tuple.class);
Expression<Long> subqCount;
{
Root<MyEntity> root = subQ.from(MyEntity.class);
Path<MyEntity> filter = root.get(MyEntity.challenge);
subqCount = cb.count(root);
// How to select tuple?
Selection<Tuple> tuple = cb.tuple(filter, subqCount);
// !! Run-time exception here
Expression<Tuple> tupleExpr = (Expression<Tuple>) tuple;
// Not sure why I can't use multiSelect on a subQuery
// #select only accepts Expression<Tuple>
createSubQ.select(tupleExpr);
createSubQ.groupBy(filter);
}
cq.multiselect(subqCount);
Although the compiler doesn't complain, I still get a run-time exception.
java.lang.ClassCastException: org.hibernate.ejb.criteria.expression.CompoundSelectionImpl cannot be cast to javax.persistence.criteria.Expression
- Is this a bug in hibernate, or am I doing something wrong?
- If you can't use
multiselect
on a subquery, then how can you perform agroupBy
? - If you can't use
groupBy
on a subquery, why is it in the API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有同样的问题。
我只能尝试回答你的最后一个问题,说你只能真正使用子查询来执行非常简单的查询,例如:
我想说的另一件事是这个事件让我想起了xkcd #979。
I have the same problem.
I can only attempt to answer your last question by saying you can only really use sub queries to perform very simple queries like:
The other thing I wanted to say was how much this incident reminds me of xkcd #979.
我有类似的问题。
我有规范,并且我想获取与该规范匹配的对象的 id。
我的解决方案:
首先我选择列(在我的情况下我只需要一列),然后调用 where 方法与我给定的规范合并。
I had similar problem.
I had specification, and I wanted to get ids of objects matching this specification.
My solution:
First I select columns (In my case I need only one column), and then I call where method to merge with my given specification.