简体   繁体   中英

Trying to get distinct results, saying string cannot be cast to blah.Model.Article

Trying to get all articles, with unique titles (distinct(title)), that have a body of "".

List<Article> articles = (List<Article>) session.createQuery("select distinct a.title from Article a where body = :body")
.setString("body", "")
.list();

I am getting the error:

main java.lang.ClassCastException: java.lang.String cannot be cast to blah.Model.Article Exception in thread "main" java.lang.ExceptionInInitializerError

The Article table has duplicates in it, that is why I am trying to get only unique articles. It doesn't matter which once it gets, as long as the title is unique, and the body is "".

Could I use a subquery to get the results? 是否可以使用子查询获取结果?

You're selecting the titles , not the articles - which is why you're getting strings back.

The query of "articles with distinct titles" doesn't even make sense - if you have two articles which share a title, which would you expect to be returned?

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