如何对Entity-Bean @OneToMany关系应用默认限制 [英] How to apply a default-restriction on Entity-Bean @OneToMany Relationships
问题描述
我有两个实体模型,客户模型和订单模型.每个客户可能有数千个订单.这两个实体之间存在OneToMany和ManyToOne关系.
I have two entity models, Customer and Order. Each customer could have thousands of orders. I have a OneToMany and ManyToOne relationship between these two entities.
如何将这种关系的列表限制为仅排名前10位的订单?
How do I restrict this relationship's list to only top 10 orders?
是否可以将"WHERE"条件用作@OneToMany上的属性?
Is it possible to apply 'WHERE' condition as an attribute on @OneToMany or not?
赞:
@OneToMany("Where Order.orderNo > 100")
我的问题是,当由实体管理器创建的对象的所有订单都在内存中创建时. 延迟加载无法解决问题,因为我需要在默认结构中获得前十名的订单.
My problem is when the object created by Entity Manager all Orders are created in memory. Lazy loading can not solve my consideration, because I need to get top 10 orders in default construction.
推荐答案
我的意思是说是否可以将'WHERE'条件用作@OneToMany上的属性?
I mean if it is possible to apply 'WHERE' condition as an attribute on @OneToMany or not?
不适用于标准JPA.但是某些提供商对此进行了扩展.例如,Hibernate确实具有@Where
批注:
Not with standard JPA. But some providers have extensions for this. For example, Hibernate does have a @Where
annotation:
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@Where(clause="1=1")
public Set<Ticket> getTickets() {
return tickets;
}
参考文献
- 休眠注释参考指南
- 2.4.6.集合相关的注释
- Hibernate Annotations Reference Guide
- 2.4.6. Collection related annotations
这篇关于如何对Entity-Bean @OneToMany关系应用默认限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
References