Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ebean query using setDistinct() does not work

I'm using an ebean query in the play! framework to find a list of records based on a distinct column. It seems like a pretty simple query but the problem is the ebean method setDistinct(true) isn't actually setting the query to distinct.

My query is:

List<Song> allSongs = Song.find.select("artistName").setDistinct(true).findList();

In my results I get duplicate artist names.

From what I've seen I believe this is the correct syntax but I could be wrong. I'd appreciate any help. Thank you.

like image 793
jacobduron Avatar asked Oct 20 '13 00:10

jacobduron


2 Answers

I just faced the same issue out of the blue and can not figure it out. As hfs said its been fixed in a later version but if you are stuck for a while you can use

findSet()

So in your example use

List<Song> allSongs = Song.find.select("artistName").setDistinct(true).findSet();
like image 117
Shawn Vader Avatar answered Oct 18 '22 17:10

Shawn Vader


According to issue #158: Add support for using setDistinct (by excluding id property from generated sql) on the Ebean bug tracker, the problem is that an ID column is added to the beginning of the select query implicitly. That makes the distinct keyword act on the ID column, which will always be distinct.

This is supposed to be fixed in Ebean 4.1.2.

like image 20
hfs Avatar answered Oct 18 '22 16:10

hfs