简体   繁体   中英

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 questions.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM