Currently I have a child entity that has a @ManyToOne
association to it's parent entity. Previous developers have set this field as lazy="false"
to get the parent whenever needed when the session is closed too, however I decided it should be lazy="true"
as it's not always used but when doing so I ran into LazyInitializationException
because the session is closed and the child is detached from the session when it tries to get the parent.
I was wondering if it's right to move some more logic of the run
method as seen bellow to the service class which interacts with DAO
s thus I could avoid the exception because currently the service classes are like plain classes which have the needed DAO
s injected and they just call the DAO
method and returns the result.
Should I put like more methods in the service class which interact with the entities, which would get the user and check everything for log in action, get parent if needed and then just return the log in result to run
method..
public class Login extends Runnable {
private UserService userService;
...
public void run() {
...
User user = userSerivce.getById(id);
Account account = user.getAccount(); //LazyInitializationException
...
if (account.isLocked()) {
...
}
...
userService.save(user);
//Send some message to the user..
}
}
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
...
public User getById(long id) {
return userDAO.getById(id);
}
public void save(User user) {
userDAO.save(user);
}
}
public UserDAOImpl implements UserDAO {
private SessionFactory factory;
...
public User getById(long id) {
return (User) factory.getCurrentSession().load(User.class, id);
}
public void save(User user) {
factory.getCurrentSession().saveOrUpdate(user);
}
}
I use Spring's <tx:advice>
to handle the closing and other transaction related stuff.