Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按引用实体中的第一项排序JPA存储库响应_Java_Jpa_Spring Data_Eclipselink_Jpql - Fatal编程技术网

Java 按引用实体中的第一项排序JPA存储库响应

Java 按引用实体中的第一项排序JPA存储库响应,java,jpa,spring-data,eclipselink,jpql,Java,Jpa,Spring Data,Eclipselink,Jpql,我简化了这些JPA实体: @Entity class Trip { @Id Long tripId; String travelerName; @OneToMany List<TripLeg> tripLegs; } @Entity class TripLeg { @Id Long tripLegId; Calendar departureDate; @ManyToOne Trip trip;

我简化了这些JPA实体:

@Entity
class Trip {
    @Id
    Long tripId;

    String travelerName;

    @OneToMany
    List<TripLeg> tripLegs;
}

@Entity
class TripLeg {
    @Id
    Long tripLegId;

    Calendar departureDate;

    @ManyToOne
    Trip trip;
}
将按此进行排序:

{
    id : 2,
    travelerName : "John Doe",
    tripLegs : [{
            id : 1,
            departureDate : 2015-10-01
        }, {
            id : 2,
            departureDate : 2015-10-05
    }]
}

您可以修改findByTravelerName方法来实现这一点

Page<Trip> findByTravelerNameOrderByTripLegsDepartureDateAsc(String travelerName, Pageable pageable);

您可以修改findByTravelerName方法来实现这一点

Page<Trip> findByTravelerNameOrderByTripLegsDepartureDateAsc(String travelerName, Pageable pageable);

您可以尝试使用子查询查找trip的最小偏差,然后将其用作trip.tripLegs的筛选器

差不多

从trip trip LEFT JOIN trip.tripLegs legs中选择不同的trip 其中legs.departureDate=从TripLeg l中选择Minl.departureDate 其中L.trip=按航段的行程顺序。出发日期


您可以尝试使用子查询查找trip的最小偏差,然后将其用作trip.tripLegs的筛选器

差不多

从trip trip LEFT JOIN trip.tripLegs legs中选择不同的trip 其中legs.departureDate=从TripLeg l中选择Minl.departureDate 其中L.trip=按航段的行程顺序。出发日期


和我讨论这个问题的人指出了一个显而易见的可能性——只是提出一个观点。这就是我最后做的。我对JPA解决方案的关注太多了,因此我没有看到有意义的替代方案。

与我讨论这一问题的人指出了一个显而易见的可能性——只是提出一个观点。这就是我最后做的。我太专注于JPA解决方案了,我没有看到有意义的替代方案。

这在页面中为每个三元组添加了重复记录-即,结果页面从2次到3次,每个三元组我们排序。Sam Jones用查询更新了答案,但是,请先测试它或尝试将其转换为sql,这会在页面中为每个三元组添加重复记录-即,结果页面从2次到3次,我们按每个三元组排序。Sam Jones使用查询更新了答案,但请先测试它或尝试将其转换为sql
Page<Trip> findByTravelerNameOrderByTripLegsDepartureDateAsc(String travelerName, Pageable pageable);
SELECT trip FROM Trip as trip join trip.tripLegs as tleg group by tleg.trip_id order by tleg.departureDate ASC