Subselect not used when batch loading by id in Hibernate

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 6
  • Hibernate 4

Issue

  • An entity class is batch enabled and is using SUBSELECT fetch.

    @Entity
    @BatchSize(size = 1000)
    public class EmployeeGroupSubselect {
        ...
        @OneToMany(cascade = CascadeType.ALL)
        @Fetch(FetchMode.SUBSELECT)
        private List<Employee> employees = new ArrayList<Employee>();
        ....
    }
    
  • When fetching a set of entities and their associations, subselect is not used to fetch the associations.

  • For each entity, a separate query is issue to fetch the association

    select
        employees0_.EmployeeGroupSubselect_departmentName as Employee1_3_0_,
        employees0_.EmployeeGroupSubselect_groupName as Employee2_3_0_,
        employees0_.employees_id as employee3_3_0_,
        employee1_.id as id1_1_1_,
        employee1_.name as name2_1_1_ 
    from
        EmployeeGroupSubselect_Employee employees0_ 
    inner join
        Employee employee1_ 
            on employees0_.employees_id=employee1_.id 
    where
        employees0_.EmployeeGroupSubselect_departmentName=? 
        and employees0_.EmployeeGroupSubselect_groupName=?
    

Root Cause

This is a known defect - This content is not included.BZ-1326114

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.