List<Order> orders = Ebean.find(Order.class)
.select("id, status, orderDate") // just some columns to that the sql is a bit smaller
.where().gt("details.orderQty", 0) // predicate on a @OneToMany property
.findList();
// just run it raw to get a flat sql resultSet
List<SqlRow> list0 = Ebean.createSqlQuery("select distinct
t0.id , t0.status , t0.order_date "
+" from o_order t0 join o_order_detail u1 on u1.order_id =
t0.id where u1.order_qty > 0")
.findList();
// now remove the distinct ...
List<SqlRow> list1 = Ebean.createSqlQuery("select
t0.id , t0.status , t0.order_date "
+" from o_order t0 join o_order_detail u1 on u1.order_id =
t0.id where u1.order_qty > 0")
.findList();
// this will output for my case 3 rows
System.out.println("list0: "+list0);
// this will output for my case 7 rows ... and you will see duplicate rows
System.out.println("list1: "+list1);
So then you've got to figure out why the distinct (3 rows) is actually what we want (and that the 7 rows with duplicates is actually not what we want). So yes, Ebean is adding the distinct based on the fact it knows the predicate is on a OneToMany (or ManyToMany) path - the important point being the cardinality of the relationship.
02:30:22.976 [main] TRACE org.avaje.ebean.SQL - txn[1004] select distinct
t0.id c0, t0.status c1, t0.order_date c2 from o_order t0 join o_order_detail u1 on u1.order_id =
t0.id where u1.order_qty > ? ; --bind(0)
02:30:22.988 [main] DEBUG org.avaje.ebean.SUM - txn[1004] FindMany type[Order] origin[C6KxRF.DWeHD5.BvQ02m] exeMicros[25385] rows[3] name[] predicates[u1.order_qty > ? ] bind[0]
02:30:22.992 [main] TRACE org.avaje.ebean.TXN - txn[1004] Commit - query only
02:30:22.999 [main] TRACE org.avaje.ebean.SQL - txn[1005] select distinct
t0.id , t0.status , t0.order_date from o_order t0 join o_order_detail u1 on u1.order_id =
t0.id where u1.order_qty > 0; --bind()
02:30:23.016 [main] DEBUG org.avaje.ebean.SUM - txn[1005] SqlQuery rows[3] time[18] bind[]
02:30:23.017 [main] TRACE org.avaje.ebean.TXN - txn[1005] Commit - query only
02:30:23.019 [main] TRACE org.avaje.ebean.SQL - txn[1006] select
t0.id , t0.status , t0.order_date from o_order t0 join o_order_detail u1 on u1.order_id =
t0.id where u1.order_qty > 0; --bind()
02:30:23.031 [main] DEBUG org.avaje.ebean.SUM - txn[1006] SqlQuery rows[7] time[12] bind[]
02:30:23.032 [main] TRACE org.avaje.ebean.TXN - txn[1006] Commit - query only
list0: BeanList size[3] list[{id=2, status=2, order_date=2014-12-10}, {id=1, status=0, order_date=2014-12-10}, {id=3, status=3, order_date=2014-12-10}]
list1: BeanList size[7] list[{id=1, status=0, order_date=2014-12-10}, {id=1, status=0, order_date=2014-12-10}, {id=1, status=0, order_date=2014-12-10}, {id=2, status=2, order_date=2014-12-10}, {id=3, status=3, order_date=2014-12-10}, {id=3, status=3, order_date=2014-12-10}, {id=3, status=3, order_date=2014-12-10}]