JPA/Hibernate +从 onetomayrelation 获取特定项目
我想获取 OneToMany 关系中的特定行。例如,获得订单中最便宜的商品
示例:
public class Order {
@Id
@Column(name = "ORDER_ID")
private Long id;
???
private Item cheapestItem;
}
public class Item {
@Id
@Column(name = "ITEM_ID")
private Long id;
private Long price;
}
我该怎么做?
I want to get a specific row in a OneToMany relation. E.g. getting the cheapest item of an order
Example:
public class Order {
@Id
@Column(name = "ORDER_ID")
private Long id;
???
private Item cheapestItem;
}
public class Item {
@Id
@Column(name = "ITEM_ID")
private Long id;
private Long price;
}
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在 hibernate
@Where
注释中指定 where 子句(但不确定是否可以将其应用于非集合)Try specifying a where clause in the hibernate
@Where
annotation (Not sure if you can apply it to a non-collection, though)如果您确实想获得最便宜的
Item
(而不实际保留它),那么应该是可行的带有ManyToOne
和JoinColumnOrFormula
。需要 Hibernate 3.5+,请参阅 HHH-4382 和 HHH-5041。对于以前版本的 Hibernate,仅检索价格会更加容易且可行。请参阅 Hibernate 派生属性 - 性能和可移植性。
If you really want to get the cheapest
Item
(without actually persisting it), it should be is doable with aManyToOne
and aJoinColumnOrFormula
. Requires Hibernate 3.5+, see issues like HHH-4382 and HHH-5041 for examples.Retrieving only the price would be much easier and doable with previous versions of Hibernate. See Hibernate Derived Properties - Performance and Portability.