| See the attached test case. The relevant part is bytecode enhancement in the pom file, and LazyCollectionOption.EXTRA in ParentEntity: {{@Entity public class ParentEntity { ... @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent") @LazyCollection(LazyCollectionOption.EXTRA) private Set<ChildEntity> children = new HashSet<>(); ... } }} The actual output shows that ParentEntity.children is initialized with all children: Hibernate: select parententi0_.id as id1_1_ from ParentEntity parententi0_ Hibernate: select children0_.parent_id as parent_i2_0_1_, children0_.id as id1_0_1_, children0_.id as id1_0_0_, children0_.parent_id as parent_i2_0_0_ from ChildEntity children0_ where children0_.parent_id=? The expected output should show that only the number if children is queried: Hibernate: select parententi0_.id as id1_1_ from ParentEntity parententi0_ Hibernate: select count(id) from ChildEntity where parent_id =? Suggested fix is to delay initialization in AbstractEntityPersister.initializeLazyProperty for extra-lazy collections: {{ // Initialize it unless it is extra lazy if ( !persister.isExtraLazy() ) { session.initializeCollection( collection, false ); } interceptor.attributeInitialized( fieldName ); }} The suggested fix will show the expected output. |