I got an Entity 1 -> SubEntity n One-To-Many-relation.
I just would like to check, if this is the most efficient way to search for an Entity, including a specified SubEntity in its “One-To-Many-Collection”. It works, but do I have to join fetch the SubEntities or is there a more lightweight solution if I don’t need all SubEntities to be loaded? (FetchMode = Lazy)
public Entity getEntityBySubEntity(SubEntity subEntity) {
List<Entity> result = (List<Entity>)getHibernateTemplate.findByNamedParam(
"From Entity as e left join fetch e.subEntities as sub where sub.id = :id","id",subEntity.getId());
if (!result.isEmpty()) {
return result.get(0);
} else {
throw new NoResultException();
}
}
(by the way, there should always be just one result…)
thx in adv,
cav
Your query is fine, but needlessly fetches the subEntities. Just remove the fetch keyword. And since the sub-entity can’t be null to satisfy the condition, an inner join is OK: