這是最DAO中使用的典型設置:@Transactional如何影響Hibernate中的當前會話?
@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);
}
}
我只看到getCurrentSession()
叫,沒有openSession
或close
。
因此,當我從load
返回實體時,它不在會話中,無法加載懶惰集合。同樣,保存似乎總是沖洗!
春季的@Transactional
註釋是否獨自開啓和關閉會話和交易?
是看看這個答案http://stackoverflow.com/questions/10680364/how-spring-manage-hibernate-session-lifecycle –
@ShadowRay嘿,謝謝。鏈接很好,但不涉及'@ Transactional'。你是否可以描述當你連接sessionFactory時會發生什麼,然後調用DAO的方法? –