Select distinct with Spring Hibernate Sessionfactory
this would be my query:
SELECT DISTINCT name FROM city;
this is my code at the moment:
public List<City> listCities() {
return sessionFactory.getCurrentSession().createQuery("from City").list();
}
which means:
SELECT * FROM city;
How must I change the code, so the query would be correct?
I hope I gave enough information, feel free to ask questio开发者_开发百科ns.
Simply write the following HQL:
sessionFactory.getCurrentSession().createQuery("select distinct from City").list()
or even better (with result transformer):
Query q = sessionFactory.getCurrentSession().createQuery("from City"); q.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
精彩评论