java - QueryDSL sorting not working with Spring Data -
i using jpasort
spring data commons 1.9.1 , spring jpa 1.7.1. need use querydsl because jpa not allow defining sort null values.
this repository
public interface datasheetrepository extends jparepository<datasheet, long>, jpaspecificationexecutor<datasheet>
i doing in controller:
page<datasheet> page = m_datasheetrepository.findall( new pagerequest( pagenumber, pagesize, createsortfordatasheets() ) );
this had jpa:
private sort createsortfordatasheets() { // first sort on component type name, on subtype name return new jpasort( jpasort.path( datasheet_.componentsubtype ).dot( componentsubtype_.componenttype ).dot( componenttype_.name ) ) .and( new jpasort( jpasort.path( datasheet_.componentsubtype ).dot( componentsubtype_.name ) ) ); }
this have changed querydsl:
private sort createsortfordatasheets() { return new qsort( new orderspecifier<>( order.asc, qdatasheet.datasheet.componentsubtype.componenttype.name,orderspecifier.nullhandling.nullslast ) ) .and( new orderspecifier<>( order.asc, qdatasheet.datasheet.componentsubtype.name, orderspecifier.nullhandling.nullslast ) ); }
however, nothing seems sorted. have enabled debug logging , see this:
rendered criteria query -> select generatedalias0 datasheet generatedalias0 order generatedalias0.name asc, generatedalias0.name asc
if change to:
private sort createsortfordatasheets() { return new qsort(qdatasheet.datasheet.name.desc()); }
then sorting name of "datasheet" type in reverse order work.
this "datasheet" entity:
@entity public class datasheet { // ------------------------------ fields ------------------------------ @id @generatedvalue private long id; @column(unique = true) private string name; private string description; @manytoone private componentsubtype componentsubtype; @onetomany(cascade = cascadetype.remove) private set<datasheetdocument> documents;
and "componentsubtype":
@entity public class componentsubtype { @id @generatedvalue() private long id; private string name; @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "parent_id",insertable=false,updatable=false) private componenttype componenttype;
update:
if change createsortfordatasheets
this:
private sort createsortfordatasheets() { return new qsort( new orderspecifier<>( order.asc, qcomponenttype.componenttype.id, orderspecifier.nullhandling.nullslast ) ); }
then, logging shows:
rendered criteria query -> select generatedalias0 datasheet generatedalias0 order generatedalias0.id asc
notice how id of "datasheet" used, , not id of "componenttype".
it looks spring data bug. filed issue https://jira.spring.io/browse/datacmns-621
Comments
Post a Comment