I have a very simple model built with GORM, let's say a Class Parent and a @OneToMany relation with a class Child. Child class has a 'version' attribute. Model is mostly read only as upsates are very rare. I retrieve my parent object whit a criteria query from the database and I get every relation with his children too (eager or lazy does not really matter). So at this point I have a Parent with his children.
Now, I need to filter out some Child object dynamically from the set of relations of the Parent object, according to some rule based on the 'version' attribute (example: if two children have the same name, only the latest version should be in the relation set), and to pass the result to upper layers, for further elaborations (maybe accessing the database again).
I am evaluating 2 approches:
1) A DTO approach. I build a DTO filtering out what I don't need and I pass my result DTO object to the upper layers.
2) Retrieve all that I need, detach the objects, filter out the children and pass the detached objects to the upper layers.
3) Some filter capabilities that I don't know, that can filter relations given some attribute or some criteria
I think that the first one is more secure, the database can be accessed separately, the DTO objects can be modified without risking to accidentally throw Exceptions while modifying the GORM Entities; but I would like to know if I am right and what are the recommendations when approaching a layered application that has to modify Objects/Entities before returning them to the upper layers.