KdgDev
KdgDev

Reputation: 14549

Force ebean to not include an ID in a generated query

I'm building a select that has to get me all distinct values from a table.

The sql I would normally write would look like this: "SELECT DISTINCT ARTIST FROM MUSICLIB"

However, ebean is generating the following: "SELECT DISTINCT ID, ARTIST FROM MUSICLIB"

The finder is as such:

find.select("artist").setDistinct(true).findList();

I've found that ebean is generating this ID on every single query, no matter what options I set.

How do I accomplish what I'm looking for?

Upvotes: 1

Views: 825

Answers (1)

biesior
biesior

Reputation: 55798

You can't do that, Ebean for objects mapping requires ID field, and if you won't include it you'll get some mysterious exceptions.

Instead you can query DB without mapping and then write your SQL statement yourself:

SqlQuery sqlQuery = Ebean.createSqlQuery("SELECT DISTINCT artist FROM musiclib");
List<SqlRow> rows = sqlQuery.findList();

for (SqlRow row : rows) {
    debug("I got one: " + row.getString("artist"));
}

Of course if artist is a relation, you need to perform additional query using list of found IDs with in(...) expression.

Upvotes: 1

Related Questions