I’ve got an object mapped with a ManyToOne column. When trying to order by a column on the ManyToOne joined table, the count and actual results list size are inconsistent. This allows the user to page beyond the actual last page of results. When Seam’s EntityQuery renders the SQL to retrieve the list of results it includes that table with a join, But for the COUNT SQL it’s leaving it out.
In the example SQL below, I’m ordering on table ETEM_VW_CIN_COURSE, column CIN.
The difference in the SQL comes down to:
select
...
from
ETEM.ETEM_INV_INVENTORY eteminvinv0_,
ETEM.ETEM_UIC etemuic1_,
ETEM.ETEM_VW_CIN_COURSE etemvwcinc2_
where
...
order by
etemvwcinc2_.CIN
And:
select
count(*) as col_0_0_
from
ETEM.ETEM_INV_INVENTORY eteminvinv0_,
ETEM.ETEM_UIC etemuic1_
where
...
This only happens when I’m ordering by a column on the ManyToOne join. If I remove the order by then it looks to work as expected. Any way to get the SQL and COUNT SQL to execute the same query?
The problem is caused by my trying to sort on a joined table, joined on a field which isn’t required. I adjusted my EJBQL so that when I want to sort on that column (stored as orderBy on my component), to only include rows where the foreign key is not null. That portion looks like this in my WHERE clause:
and ( lower(#{etemInvInventoryList.orderBy}) <> ‘cin’ or etemInvInventory.crseId is not null )
That forces the EJBQL and CountEJBQL to pull the same set of results.