在 Hibernate 4 中使用 Criteria API 进行复杂查询
Complex query using Criteria API in Hibernate 4
我正在使用符合标准 API 的 hibernate 4,我面临一个复杂的查询。
我的关系模型如下:
我想要做的是为给定的人获取属于 "n" 最后 ShoppingEvent
之前指定的所有已发货 文章 日期。
如何使用条件 API 实现该目标?
n.b. 我已经试过了:
ProjectionList properties = Projections.projectionList();
properties.add(Projections.property("article.articleId"), "articleId");
properties.add(Projections.property("article.price"), "price");
properties.add(Projections.property("article.type"), "type");
return session.createCriteria(Person.class)//
.add(Restrictions.idEq(person.getPersonId()))//
.createAlias("articles", "article")//
.createAlias("article.shoppingEvent", "se")//
.add(Restrictions.le("se.date", currentDate))//
.addOrder(Order.desc("se.date"))//
.setProjection(properties)//
.setResultTransformer(Transformers.aliasToBean(Articles.class))//
.list();
我想要的returns篇文章没成功用setMaxResults
限制最大篇数ShoppingEvents
.
如果您根据文章构建查询会更容易,就像这样:
ProjectionList properties = Projections.projectionList();
properties.add(Projections.property("id"), "articleId");
properties.add(Projections.property("price"), "price");
properties.add(Projections.property("type"), "type");
return session.createCriteria(Article.class)
.createAlias("person", "person")
.createAlias("shoppingEvent", "shoppingEvent")
.add(Restrictions.idEq(person.getPersonId()))
.add(Restrictions.le("shoppingEvent.date", currentDate))
.addOrder(Order.desc("shoppingEvent.date"))
.setProjection(properties)
.setResultTransformer(Transformers.aliasToBean(Articles.class))
.setMaxResults(1)
.uniqueResult();
I mean I get the "n" last Articles but not all the Articles of the
last "n" ShoppingEvent
要做到这一点,只需在 .setMaxResults( n )
中指定 "n"
您可能想尝试 DetachedCriteria,让它 return 您的 "n" 上次 ShoppingEvent 的 ID,然后在您的条件中添加一个限制,即您的 article.shoppingEvent 必须与来自您的 detachedCriteria 的 ID 列表。
我正在使用符合标准 API 的 hibernate 4,我面临一个复杂的查询。
我的关系模型如下:
我想要做的是为给定的人获取属于 "n" 最后 ShoppingEvent
之前指定的所有已发货 文章 日期。
如何使用条件 API 实现该目标?
n.b. 我已经试过了:
ProjectionList properties = Projections.projectionList();
properties.add(Projections.property("article.articleId"), "articleId");
properties.add(Projections.property("article.price"), "price");
properties.add(Projections.property("article.type"), "type");
return session.createCriteria(Person.class)//
.add(Restrictions.idEq(person.getPersonId()))//
.createAlias("articles", "article")//
.createAlias("article.shoppingEvent", "se")//
.add(Restrictions.le("se.date", currentDate))//
.addOrder(Order.desc("se.date"))//
.setProjection(properties)//
.setResultTransformer(Transformers.aliasToBean(Articles.class))//
.list();
我想要的returns篇文章没成功用setMaxResults
限制最大篇数ShoppingEvents
.
如果您根据文章构建查询会更容易,就像这样:
ProjectionList properties = Projections.projectionList();
properties.add(Projections.property("id"), "articleId");
properties.add(Projections.property("price"), "price");
properties.add(Projections.property("type"), "type");
return session.createCriteria(Article.class)
.createAlias("person", "person")
.createAlias("shoppingEvent", "shoppingEvent")
.add(Restrictions.idEq(person.getPersonId()))
.add(Restrictions.le("shoppingEvent.date", currentDate))
.addOrder(Order.desc("shoppingEvent.date"))
.setProjection(properties)
.setResultTransformer(Transformers.aliasToBean(Articles.class))
.setMaxResults(1)
.uniqueResult();
I mean I get the "n" last Articles but not all the Articles of the last "n" ShoppingEvent
要做到这一点,只需在 .setMaxResults( n )
中指定 "n"您可能想尝试 DetachedCriteria,让它 return 您的 "n" 上次 ShoppingEvent 的 ID,然后在您的条件中添加一个限制,即您的 article.shoppingEvent 必须与来自您的 detachedCriteria 的 ID 列表。