开发者

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜