Related to: How to countDistinct on multiple columns
I have an entity class that contains many fields, three of which are longitude
, latitude
and update_time
. I am trying to add a @Formula
field that concatenates the three:
@Formula("concat(longitude, latitude, update_time)")
public String fix;
I would then like to use that field as part of a countDistinct
query:
@SuppressWarnings( {"unchecked", "rawtypes"} )
public long getCountDistinctPositions() {
Session session = sessionFactory.openSession();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
Root<Position> position = criteriaQuery.from(Position.class);
Expression fix = position.get("fix");
Expression countDistinct = criteriaBuilder.countDistinct(fix);
criteriaQuery.select(countDistinct);
Query query = session.createQuery(criteriaQuery);
Long result = (Long)query.getSingleResult();
session.close();
return result;
}
But I keep getting an exception:
java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [fix] on this ManagedType [aaa.Position]