hibernate openSession和getCurrentSession区别和联系、get和load的区别

Part 1 openSession和getCurrentSession区别

1、采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会 
2、采用getCurrentSession()创建的session在commit或rollback会自动关闭,而采用openSession()必须手动关闭 
3、getCurrentSession()在查询时也需使用事务。 
4、使用getCurrnetSession需在hibernate.cfg.xml中配置。

Part 2 openSession和getCurrentSession联系

用ThreadLocal模式(线程局部变量模式)管理Session(不用hibernate.cfg.xml)则与getCurrentSession相同。

Part 3 get()和load()的区别

1、get()方法直接返回实体类,如果查不到数据则返回null,load()会返回一个实体对象,但当代理对象被调用,如果数据不存在,会报错。 
2、load()在默认情况下支持延迟加载(lazy)。 
3、总之,如果确定DB中有这个对象用load(),不确定用get().

你可能感兴趣的:(hibernate openSession和getCurrentSession区别和联系、get和load的区别)