hibernate opensession()和getCurrentSession()
来源:互联网 发布:剑雨江湖进阶数据仙器 编辑:程序博客网 时间:2024/05/17 00:02
public void teacherSave(){ Teacher t=new Teacher(); t.setName("ttt"); t.setTitle("high"); t.setBirthDate(new Date()); Configuration con=new AnnotationConfiguration(); SessionFactory sf=con.configure().buildSessionFactory();//Session session=sf.openSession();/** openSession和getCurrentSession的区别:(重要)* openSession永远是打开新的session,Session session2=sf.openSession()。需要close();* System.out.println(session==session2);打印出的是false;* 而getCurrentSession如果当前环境上下文存在session,getCurrentSession拿原来存在的session,没有的话或者* session.getTransaction().commit()提交之后才会创建session。不用自己在close();* * getCurrentSession()的功能要从ThreadLocal类说起,ThreadLocal是一个线程作用范围的对象,就是说保存在他中的* 对象的生命周期是一个线程,Spring调用该方法后会将打开的Session放入ThreadLocal中进行保存,然后通过getCurrentSession()* 方法返回保存的Session对象,这样在一次请求(一个线程)中调用getCurrentSession()就都会从ThreadLocal中取出保存的Session对* 象,这样就保障了一次请求(一个线程)中使用的都是同一个Session对象了,可以保障事务操作的特性,像数据一致性和数据安全性等*/ Session session=sf.getCurrentSession(); session.beginTransaction(); session.save(t); System.out.println(t.getId()+"===="); session.getTransaction().commit(); //session.close(); //sf.close(); }}
对象的三种状态?
在Hibernate中,对象有三种状态:临 时状态(Transient)、持久状态(Persistent)和游离状态(Detached)。
1. 临时状态
由 new命令开辟内存空间的java对象,例如:
User user=new User();
临 时对象在内存孤立存在,它是携带信息的载体,不和数据库的数据有任何关联关系.
(a) 如 果没有变量对该对象进行引用,它将被gc回收;
(b) 在Hibernate中,可通过 session的save()或saveOrUpdate()方法将瞬时对象与数据库相关联,并将数据对应的插入数据库中,此时该临时对象转变成持久化对 象.
- 持久状态
处于该状态的对象在数据库中具有对应的记录,并拥有一个持久化标识.通过session的get()、load() 等方法获得的对象都是持久对象。
持久化对象被修改变更后,不会马上同步到数据库,直到数据库事务提交。在同步之前,持久化 对象是脏的(Dirty)。
(a) 如果是用hibernate的delete()方法,对应的持久对象就变成临时对象,因数据库中 的对应数据已被删除,该对象不再与数据库的记录关联.
(b) 当一个session执行close()或 clear()、evict()之后,持久对象变成游离对象,此时该对象虽然具有数据库识别值,但它已不在HIbernate持久层的管理之下.
持久对象具有如下特点:
(1)和session实例关联;
(2)在数据库中有与之关联的记录,并 拥有持久化标识.
- 游离状态
当与某持久对象关联的session被关闭后,该持久对象转变为游离对象.当游离对象被重新关联到session上 时,又再次转变成持久对象(在Detached其间的改动将被持久化到数据库中)。 游离对象拥有数据库的识别值,但已不在持久化管理范围之内。
(a) 通 过update()、saveOrUpdate()等方法,游离对象可转变成持久对象.
(b) 如果是用hibernate的delete()方法,对应的游离对象就变成临时对象,因数据库中的 对应数据已被删除,该对象不再与数据库的记录关联.
- hibernate openSession和getCurrentSession
- hibernate opensession()和getCurrentSession()
- Hibernate中的getCurrentSession和openSession
- Hibernate之openSession和getCurrentSession
- Hibernate中openSession 和 getCurrentSession
- Hibernate中openSession 和 getCurrentSession
- hibernate openSession和getCurrentSession区别
- HIbernate中openSession和getCurrentSession
- hibernate之opensession和getCurrentSession
- Hibernate之openSession和getCurrentSession
- spring+Hibernate openSession()和getCurrentSession()
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- hibernate的session判断-openSession和getCurrentSession
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- group by cube
- XMPP 群的设计和讨论
- 中国云计算论坛
- Maven之生成测试覆盖度的插件cobertura-maven-plugin的使用
- truct in6_addr has no member named in6_u
- hibernate opensession()和getCurrentSession()
- POJ - 2229
- 【BZOJ1132】【POI2008】Tro 计算几何 叉积求面积
- nedmalloc
- Java笔记--1
- Android JNI调用的二种方式
- mahout 聚类实例
- Qt 出现error: expected constructor, destructor, or type conversion before ‘typedef’
- SAT阅读真题考什么?