In my web application I have an Object with a OneToMany relationship with Child. When selecting my Objects I execute the following query:
from Object o
and then i should print for every object how many children it has with
// Foreach
object.children.size()
Assuming the object has a lot of children (let's say 30'000); Is it a waste of resources calling size() or the ORM framework (in my case Hibernate) will take care of that without loading all the children?
Using JPA (the standard):
Entity.getCollection().size()
would trigger lazy loading to retrieve all of the child collection - so yes, it would be fairly slow, unless you needed to operate on all/most elements anyway. Note: for all (sane) implementations of JPA, this will NOT issue 30,000 seperate queries - it will issue one query that returns 30,000 rows in the result set. @OneToMany(fetch=FetchType.EAGER)
EntityManager.getQuery()/getTypedQuery()/getNamedQuery()
(or even SQL via getNativeQUery()
). This is quite simple and highly performant: int empSize = em.createQuery("SELECT SIZE(d.employees) FROM Department d") .getSingleResult();
OR ALTERNATIVELY
// Pre-compiled query placed against entity class for highest performance
@NamedQueries({
@NamedQuery(name="Department.EmployeeSize",
query="SELECT SIZE(d.employees) FROM Department"),
... // other named queries
})
@Entity
public class Department {
...
}
// Then to use the query:
int empSize = em.createNamedQuery("Department.EmployeeSize", Integer.class)
.getSingleResult();
You can enable level 2 caching. JPA Caching Summary
To statically configure level 2 caching:
Map propertiesMap = new HashMap(); // Valid values are ALL, NONE, ENABLE_SELECTIVE, DISABLE_SELECTIVE propertiesMap.add("javax.persistence.sharedCache.mode", "ENABLE_SELECTIVE"); EntityManagerFactory = Persistence.createEntityManagerFactory("myPUName", propertiesMap);
ALTERNATIVELY use persistence.xml:
<persistence-unit name="EmployeeService">
...
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
</persistence-unit>
Then mark which entities should be automatically cached in the level 2 cache:
@Cacheable(true)
@Entity
public class Employee {
...
}
Using Proprietary approach (e.g. Hibernate "Extra-Lazy" collections):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With