views:

63

answers:

1

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

Update Could I use a subquery to get the results?

+2  A: 

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?

Jon Skeet
the table has duplicates in it, and it doesn't really matter which one I get back.
mrblah
@mrblah: Hmm. I know how I'd do that in LINQ, but I don't know what it would look like in Hibernate or SQL...
Jon Skeet