I have a one-to-many collection with lazy="extra"
attribute.
Class Foo {
List<Bar> barList;
}
I have a class with lazy="false"
initialization which contains mentioned class collection.
Class FooContainer {
List<Foo> fooList;
}
I get FooContainer entity with service, so I will need to use .size()
out of transaction;
FooContainer fc = serviceImpl.getFCById(fooContainerId);
This way fc.getFooList() is initialized. Then I need to get only the size of barList
. I do not need to fetch whole collection.
for(Foo currentItem: fc.getFooList()) {
int barSize = currentItem.getBarList().size();
}
This is when LazyInitializationException appears. I tried to call this method.
@Transactional
Class AnotherServiceImpl {
int getBarListSize(Foo foo){
return foo.getBarList().size();
}
}
But it didn't work because there is no session.
I can do getHibernateTemplate().getCurrentSession.initialize(foo.getBarList())
in AnotherServiceImpl
and this will work, but I probably will fetch all items, which is unacceptable.
Is there a way to get collection size without fetching?