I have an entity A that has a foreign key of entity B:
entity A --> id, entity_a_name, foreign_key_entity_B
When I call
return session.createCriteria(EntityA.class).list();
I get the property of entityB inside entity A as well. How do I make it lazy load so it will not load enityB if not needed?
It’s unclear from your description what type of relationship you are talking about, but if it is Many-to-One or One-to-One, things aren’t so straightforward. If
A.entityBis nullable (non-optional) then Hibernate is forced to eager-load the relationship in order to see if the property is null. Only by marking the relationship as non-optional (in which case Hibernate assumes that it isn’t null since it is an error otherwise) can you make it load lazily.