views:

42

answers:

1

Dear all, I have to e ntity model like Customer and Order. Each customer could have thousand orders, well, I make OneToMany and ManyToOne relationship between these to entities. But the thing I want is how to restrict this relationship's list to only top 10 orders.

I mean if it is possible to apply 'WHERE' condition as an attribute on @OneToMany or not? Like:

@OntToMany("Where Order.orderNo > 100")

My problem is when the object created by Entity Manager all Orders created in memory. Lazy loading can not solve my consideration, because I need to get top 10 orders in default construction.

Regards

+2  A: 

I mean if it is possible to apply 'WHERE' condition as an attribute on @OneToMany or not?

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;
}

References

Pascal Thivent
What should we do for standard JPA?
Navid
@Navid: Apart from using a JPQL query, nothing, JPA does not provide support for this.
Pascal Thivent