我有一个实体类,其中有一个嵌入式对象:
@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 似乎不适合这项工作。有没有人有任何想法?
提前致谢。