Hibernate Criteria,根据最新事件选择顺序
我在休眠中有这样的类:
@Entity
class Order{
private MyPattern pat;
@Id
private int id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "order_id")
private List<Event> events;
public DetachedCriteria getCriteria() {
//here I create criterias
}
}
@Entity
class Event{
@Column
@Temporal(value = javax.persistence.TemporalType.DATE)
private Date date;
@Id
private int id;
@Column
private String name;
}
我需要的是从 MyPattern 按顺序创建 DetachedCriteria (详细结构并不重要)。我已经部分实现了这一点,但我现在的问题是仅选择具有最新 event.name 的订单,例如模式中的订单。我认为也许选择具有最新日期的行会有所帮助,但我只是不知道如何在标准中执行此操作。因此,我愿意接受解决方案和帮助。感谢
编辑: 我有一个请求,即订单。我必须用正确的订单实例进行响应(根据模式的内容)。例如:客户仅请求已发货的订单。所以我需要选择具有名为“Shipped”的最新事件的订单。
DetachedCriteria dc=DetachedCriteria.forClass(Order.class,"or").CreateAlias("events","eve");
dc.add(Restriction.eq("eve.name","Shipped"));
orders=dc.getExecutableCriteria(session).list();
基本上,这段代码应该可以解决问题,但它有一个很大的缺陷。它甚至返回“已交付”的订单,因为订单中的事件是包含订单上的每个事件的列表。因此,也许可以简单地修复,例如“选择具有 event.name=Shipped 但不是 event.name=Delivered 的订单。
I have classes in hibernate like this:
@Entity
class Order{
private MyPattern pat;
@Id
private int id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "order_id")
private List<Event> events;
public DetachedCriteria getCriteria() {
//here I create criterias
}
}
@Entity
class Event{
@Column
@Temporal(value = javax.persistence.TemporalType.DATE)
private Date date;
@Id
private int id;
@Column
private String name;
}
What I need is to create DetachedCriteria in Order from MyPattern(detail structure is not important). I have this partially implemented, but my problem now is to select only Orders that have newest event.name like one in pattern. I think maybe selecting row with nevest date would help, But I just cant figure out, how to do this in Criteria. So I am open to solutions and help. Thanks
edit:
I have request, which is Order. I have to respond with correct Order instance(according to content of pattern). For example: Client requesting only orders which was already shipped. So I need to select Orders which has newest Event with name "Shipped".
DetachedCriteria dc=DetachedCriteria.forClass(Order.class,"or").CreateAlias("events","eve");
dc.add(Restriction.eq("eve.name","Shipped"));
orders=dc.getExecutableCriteria(session).list();
Basicky this code should do the trick but it has one BIG flaw. It returns even orders which were "Delivered" because events in Order is List which contains every event on order. So maybe simple fix like "select orders which has event.name=Shipped but NOT event.name=Delivered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据事件名称模式获取结果按日期订购。事件_& Order_ 是 Event 和 Order_ 的元模型类。订单实体。
Fetching results based on event name pattern & ordered by date. Event_ & Order_ are metamodel classes of Event & Order entity.