I have an existing working query that selects a column from an entity mapped to an Oracle View using the following JPQL
SELECT COUNT(o.id) FROM MyEntityView o
I refactored it to use the JPA 2 Criteria API with the following code:
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);
....
But it generates the following error on creating the select statement:
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)
I tried changing the mapping to a table instead of a view and the it does work properly.
Is this a Hibernate Bug or am I missing something?