Quote:
I was definitely able to access ClassB's id value without any issue, so the data is in fact being loaded up and I have no idea why when all the things are set to lazily load.
If you're inside the same session, accessing ClassB's id value will cause hibernate to fetch the ClassB entities from the database. Perhaps this is what you're seeing.
I notice that your getAllClassA method only closes the session when an exception occurs so perhaps the session is still active. Is this really what you want?
With your example code in a simple test case everything worked as expected. ClassB's were only loaded when getClassB was true.
On a general note, with the Collections API you should really use the interface types for everything except instantiating a new Collection.
e.g.
public ArrayList<ClassA> getAllClassA(boolean getClassB)
Should be
public List<ClassA> getAllClassA(boolean getClassB)
e.g.
public class ClassA{
private ArrayList<ClassB> classBList = new ArrayList<ClassB>();
Should be
public class ClassA{
private List<ClassB> classBList = new ArrayList<ClassB>();
I got a hibernate error because of this when trying to run your example code:
expected type: java.util.ArrayList,
actual value: org.hibernate.collection.PersistentBag