This is a typical setup used in most DAO:
@Transactional
@Repository
public class DAO {
@Autowired
SessionFactory sessionFactory;
public void save(Entity o) {
sessionFactory.getCurrentSession().save(o);
}
public Entity load(int id) {
return (Entity)sessionFactory.getCurrentSession().get(Entity.class, id);
}
}
I see only getCurrentSession()
called, no openSession
or close
.
So when I return the entity from load
it's not in session, lazy collections cant be loaded. Similarily, save seems to always flush!
Does the @Transactional
annotation from spring do the magic of opening and closing sessions AND transactions all by itself?