I have some OneToMany
child collections in my domain classes that may grow over time to hold hundreds or (very likely) thousands of instances. However, often times the only thing the parent really needs is the "first" child or the "last" child or the "biggest" child. Having the parent instance loop through a large collection of objects loaded from the database seems inefficient.
On the other hand, I do not want to pollute my domain model with persistence concerns and start having to use my DAO's within the domain classes to execute queries.
I might potentially put these lookups in my service methods, but I really prefer to put this login in my domain where it belongs -- trying to avoid the "anaemic domain" anti-pattern.
Is there a way to pull just a "certain" object from a large collection without directly invoking the DAO? Some JPA ORM mapping capability I've overlooked?
Edit: The application is design in layers with the domain model layer at the bottom -- it depends on nothing else. Next to it is the persistence layer which implements DAOs and depends on the domain layer. Above both of them is a service layer which I am trying to keep as thin as possible by push business logic down into the domain layer.