Reputation: 342
Hi I have one question and I don't know, if it is possible. I have one Dao. I have entity where I have defined classic OneToMany assoc.
@OneToMany
public Set<InterviewEntity> getCreatedInterviews() {
return createdInterviews;
}
OneToMany -> default value of fetch is Lazy. In dao i have method findById()...
see below:
@Override
public T findById(long id) {
Session session = sessionFactory.openSession();
T entity = (T) session.get(thisClass, id);
session.close();
return entity;
}
and i would only like this:
EmployeeEntity resultEmployee = employeeDao.findById(employeeEntity.getEmployeeId());
resultEmployee.getCreatedInterviews();
I could @Override method findById in concrete Dao class with using Hibernate.initialize() but i think it's no so practical. Somebody any idea ?
Upvotes: 0
Views: 597
Reputation: 4233
Your code closes session
immediately after entity retrieval, so your resultEmployee.getCreatedInterviews()
won't work because there is no open session.
I think your problem is not using Lazy Loading, but instead coupling session management code and DAO code.
Hibernate Lazy feature is nice if you use it correctly. Try to decouple session management at business level, not DAO level. I mean, when you call some business logic from view:
If you want to move data from business to view, use VO/DTO to avoid Lazy problems.
This way you would have nice, clear and maintainable code.
Hope it helps!
Upvotes: 2