JPA/Hibernate + get specific item from onetomayrelation
I want to get a开发者_StackOverflow中文版 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?
Try specifying a where clause in the hibernate @Where
annotation (Not sure if you can apply it to a non-collection, though)
I want to get (...) the cheapest item of an order
If you really want to get the cheapest Item
(without actually persisting it), it should be is doable with a ManyToOne
and a JoinColumnOrFormula
. 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.
精彩评论