简体   繁体   中英

JPQL query- order by nested field

Given a class OP

public class OP {
  String name;
  int rank;
}

and a class D

public class D {
  OP parameter; 
}

How should be the query written in JPQL that lists the instances of D class ordered by rank field of OP class. Instances having null references to OP objects must listed at the end.

After some tests I managed to get things work. This is how I have implemented it with Spring Repository :

@Repository
public interface DRepository extends CrudRepository<D, Long> {

    @Query("select d from D d left outer join d.parameter op order by op.rank desc")
    List<D> getDs();

}

Notice the left outer join to include D instances with null parameter field and the desc after the order by clause to force instances with null OP to the end of the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM