1

我有一个实体类,其中有一个嵌入式对象:

@Entity
public class Flight implements Serializable {

    /// .... other attributes
    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "value", column =
        @Column(name = "FLIGHT_TIME")),
        @AttributeOverride(name = "dataState", column =
        @Column(name = "FLIGHT_TIME_TYPE", length = 20))
    })
    private DateDataStateValue flightDate;
}

DateDataStateValue 如下:

@Embeddable
public class DateDataStateValue implements DataStateValue<Date>, Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "DATASTATE")
    @Enumerated(value = EnumType.STRING)
    private final DataState dataState;

    @Column(name = "DATAVALUE")
    @Temporal(TemporalType.TIMESTAMP)
    private final Date value;
}

从数据库中获取航班时,使用 CriteriaQuery 并在时间列上创建 Order 对象:

Path<Flight> propertyPath = queryRoot.get("flightDate");
Order order = isAscending() ? criteriaBuilder.asc(propertyPath) :  criteriaBuilder.desc(propertyPath);

顺序不是我想要的。例如,如果飞行表具有以下值:

Flight 1  | ESTIMATED | 1 Jan 2012
Flight 2  | ESTIMATED | 1 Jan 2011
Flight 3  | ACTUAL    | 1 Jan 2010
Flight 4  | ESTIMATED | 1 Jan 2009

升序排序的结果将是:

Flight 3  | ACTUAL    | 1 Jan 2010
Flight 4  | ESTIMATED | 1 Jan 2009
Flight 2  | ESTIMATED | 1 Jan 2011
Flight 1  | ESTIMATED | 1 Jan 2012

@Embedded 列的默认排序似乎是按照元素在类中命名的顺序使用元素的自然排序。即首先是DATASTATE,然后是DATAVALUE。

我想做的是每当排序属性是飞行日期时,排序是日期,然后是状态,即:

Flight 4  | ESTIMATED | 1 Jan 2009
Flight 3  | ACTUAL    | 1 Jan 2010
Flight 2  | ESTIMATED | 1 Jan 2011
Flight 1  | ESTIMATED | 1 Jan 2012

使 DateDataStateValue 具有可比性并不会影响它,而且 @orderColumn/@OrderBy 似乎不适合这项工作。有没有人有任何想法?

提前致谢。

4

2 回答 2

0

我什至不知道您可以在这样的可嵌入属性上通过查询添加订单。但我不会依赖它,只需在您的查询中添加两个订单:

Path<Flight> statePath = queryRoot.get("flightDate.dateState"); // or queryRoot.get("flightDate").get("dateState"): to be tested
Path<Flight> valuePath = queryRoot.get("flightDate.value");
Order[] orders;
if (isAscending()) {
    orders = new Order[] {criteriaBuilder.asc(valuePath), criteriaBuilder.asc(statePath)  };
}
else {
    orders = new Order[] {criteriaBuilder.desc(valuePath), criteriaBuilder.desc(statePath)  
}

query.orderBy(orders);
于 2012-05-03T13:11:33.317 回答
0

可能类似于"flightDate.value ASC, flightDate.dataState ASC",因为您定义的只是“flightDate”,这意味着该对象的自然排序

于 2012-05-03T13:11:44.163 回答