I have a java project and I am using JPA, so I have entity classes and the correspective database tables. Everything is set and ready and is working fine.
From my database I can simply retrieve an object using a JPA query (for exemple getById()) and from this object I can get every other data I need, without using other queries. I was wondering if this is good practice or if I should use queries for getting all needed data.
Example:
First approach: Get the main object from the database using
EntityManager.getObjectById(1)and get its children usingobject.getChildren().Second approach: Get the main object from the database using
EntityManager.getObjectById(1)and get its children usingCriteriaQuery.select(Root).where(CriteriaBuilder.equal(Root.get("parent"), object))getting a ResultList from the query.
Also, would this improve or worsen performance?
I searched for a while on google and stackoverflow but I couldn't find any information. I would expect the first approach to be fine.
Just an idea, but maybe you can make some sort of method that measures how much time it takes to perform one and the other.