【发布时间】:2011-03-30 07:02:59
【问题描述】:
我有一个现有的工作查询,它使用以下 JPQL 从映射到 Oracle 视图的实体中选择一列
SELECT COUNT(o.id) FROM MyEntityView o
我通过以下代码重构它以使用 JPA 2 Criteria API:
MyEntityView model = new MyEntityView();
CriteriaBuilder criteriaBuilder = model.entityManager().getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<MyEntityView > theView = criteriaQuery.from(MyEntityView.class);
criteriaQuery.select(criteriaBuilder.count(theView.get(MyEntityView_.id))); // ERROR!
TypedQuery<Long> query = model.entityManager().createQuery(criteriaQuery);
....
但它在创建选择语句时会产生以下错误:
java.lang.NullPointerException
at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:110)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:218)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:189)
at com.mycomp.domain.view.MyEntityViewIntegrationTest.testMarkerMethod(MyEntityViewIntegrationTest.java:35)
我尝试将映射更改为表而不是视图,它确实可以正常工作。
这是 Hibernate Bug 还是我遗漏了什么?
【问题讨论】:
-
将列检索从 String 更改为 MetaModel 以防止混淆。
标签: java oracle hibernate jpa-2.0 criteria-api