休眠-如何按oneToMany集合中第一个元素的属性排序

时间:2019-09-25 10:31:22

标签: hibernate spring-data-jpa hql

使用Spring Data JPA和HQL查询,我想通过oneToMany属性中的一些timestamp属性对对象进行排序。

给出查询

select distinct t from Transport t 
join fetch t.waypoints wp  

我想做类似的事情(语法无效)

select distinct t from Transport t 
join fetch t.waypoints wp  
order by t.waypoints[0].someTimeStamp

在hql中有(相对)简单的方法吗?因为如果没有的话,用Java对结果进行排序可能会更容易(因为就我而言,列表不包含很多元素,所以没什么大不了的)

我尝试过类似的操作(Waypoint中有一个order属性)

select distinct t,wp2 from Transport t 
join fetch t.waypoints wp inner join t.waypoints wp2 
where t.id in :ids and wp2.order = 0 
order by wp2.someTimeStamp

但是,结果相乘。如果交通工具有n个航路点,则它会在结果列表中出现n次...

课程:

public class Transport {

    @Id
    private UUID id;


    @OneToMany(mappedBy = "transport")
    @OrderBy("order_ asc")
    private Set<Waypoint> waypoints = new LinkedHashSet<>();

}

@Entity
public class Waypoint {

    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name = "transport_id")
    private Transport transport;

    @Column(name = "order_")
    private Integer order;

    private Instant someTimeStamp;

}

0 个答案:

没有答案