Hibernate-如何按第一个元素排序';在oneToMany集合中的属性
使用SpringDataJPA和HQL查询,我想按照oneToMany属性中的某个时间戳属性对对象进行排序 给定查询Hibernate-如何按第一个元素排序';在oneToMany集合中的属性,hibernate,spring-data-jpa,hql,Hibernate,Spring Data Jpa,Hql,使用SpringDataJPA和HQL查询,我想按照oneToMany属性中的某个时间戳属性对对象进行排序 给定查询 select distinct t from Transport t join fetch t.waypoints wp 我想做一些类似(无效语法)的事情 在hql中是否有(相对)简单的方法来实现这一点?因为如果不是,用Java对结果进行排序可能会更容易(因为在我的例子中,列表不包含很多元素,这没什么大不了的) 我尝试过类似(Waypoint中有一个order属性) 但是
select distinct t from Transport t
join fetch t.waypoints wp
我想做一些类似(无效语法)的事情
在hql中是否有(相对)简单的方法来实现这一点?因为如果不是,用Java对结果进行排序可能会更容易(因为在我的例子中,列表不包含很多元素,这没什么大不了的)
我尝试过类似(Waypoint中有一个order属性)
但是,结果是相乘的。如果一个运输有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;
}
公共类交通工具{
@身份证
私有UUID;
@OneToMany(mappedBy=“运输”)
@订购人(“订购人”)
专用设置航路点=新LinkedHashSet();
}
@实体
公共类航路点{
@身份证
私人长id;
@许多酮
@JoinColumn(name=“transport\u id”)
私人运输;
@列(名称=“订单”)
私有整数阶;
私有即时时间戳;
}
您的实体类在哪里?我已经添加了它们,但基本上是一个简单的实体,具有一个OneToMany属性,从@OrderBy(“order”)中删除ASC
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
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;
}