开发者

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

开发者 https://www.devze.com 2022-12-15 17:03 出处:网络
Trying to get all articles, with unique titles (distinct(title)), that have a body of \"\". List<Article> articles = (List<Article>) session.createQuery(\"select distin开发者_如何转开发ct

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

List<Article> articles = (List<Article>) session.createQuery("select distin开发者_如何转开发ct 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 "".

Update 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?

0

精彩评论

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