Is there a way, using criteria queries to extract an entity as well as additional columns associated with that entity? I'm using Postgres GIS queries to determine the distance of an entity, but I can't seem to get criteria queries to do what I want. If I use native SQL, I can do this:
Code:
session.createSQL("select {myentity.*}, distance(the_geom, MakePoint(...)) as distance ...")
.addEntity("myentity", EntityClass.class)
.addScalar("distance")
.list()
But there does not seem to be a corresponding way of doing this using Criteria queries. I've managed to get this to sort of work by doing something like:
Code:
criteria.setProjection(Projections.projectionList()
.add(Projections.sqlProjection("*", new String[]{"id"}, new Type[]{Hibernate.entity(getEntityClass())}))
.add(Projections.sqlProjection(
"distance_sphere(the_geom, MakePoint(-75.25, 39.88)) as distance",
new String[]{"distance"},
new Type[]{Hibernate.DOUBLE}))
);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
But this does a query for each results returned based on ID, not the most efficient way to do what I want.
Am I relegated to building up a native SQL string? Or is there a way to get the Criteria Query API to do what I want?
Thanks!
David