views:

106

answers:

1

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?

+1  A: 

This sounds like a bug (similar to the problem reported in JPA 2 CriteriaQuery Using ID columns throws Null Pointer EX) and I couldn't find an existing Jira issue for it and suggest creating a new one.

That being said, I don't really see the point of using a CriteriaQuery here, especially if you don't use the static metamodel for full type-safety. It's maybe a simplified example though.

Pascal Thivent
Thanks, It is a simplified example. Meanwhile, I'll resort to JPQL
Joopiter