开发者

Select distinct with Spring Hibernate Sessionfactory

开发者 https://www.devze.com 2023-04-05 07:48 出处:网络
this would be my query: SELECT DISTINCT name FROM city; this is my code at the moment: public List<City> listCities() {

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

精彩评论

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