Hibernate Criteria,根据最新事件选择顺序

发布于 2024-11-05 20:21:18 字数 1109 浏览 4 评论 0原文

我在休眠中有这样的类:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-11-12 20:21:18

根据事件名称模式获取结果按日期订购。事件_& Order_ 是 Event 和 Order_ 的元模型类。订单实体。

CriteriaQuery<Order> cq = cb.createQuery(Order.class);
Root<Order> order = cq.from(Order.class);
Join<Order, Event> event = cq.join(Order_.events);
cq.select(order);
cq.where(cb.like(event.get(Event_.name)), "*somePattern"); // pattern for results
cq.orderBy(cb.asc(event.get(Event_.date));

Fetching results based on event name pattern & ordered by date. Event_ & Order_ are metamodel classes of Event & Order entity.

CriteriaQuery<Order> cq = cb.createQuery(Order.class);
Root<Order> order = cq.from(Order.class);
Join<Order, Event> event = cq.join(Order_.events);
cq.select(order);
cq.where(cb.like(event.get(Event_.name)), "*somePattern"); // pattern for results
cq.orderBy(cb.asc(event.get(Event_.date));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文