how2j.cn

下载区
文件名 文件大小
hibernate.rar 6m

工具版本兼容问题
Hibernate有两种方式获得session,分别是:
openSessiongetCurrentSession
他们的区别在于
1. 获取的是否是同一个session对象
openSession每次都会得到一个新的Session对象
getCurrentSession在同一个线程中,每次都是获取相同的Session对象,但是在不同的线程中获取的是不同的Session对象
2. 事务提交的必要性
openSession只有在增加,删除,修改的时候需要事务,查询时不需要的
getCurrentSession是所有操作都必须放在事务中进行,并且提交事务后,session就自动关闭,不能够再进行关闭


步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : OpenSession每次都会得到一个新的Session对象   
步骤 4 : 相同线程的getCurrentSession   
步骤 5 : 不同线程的getCurrentSession   
步骤 6 : openSession查询时候不需要事务   
步骤 7 : getCurrentSession所有操作都必须放在事务中   
步骤 8 : getCurrentSession在提交事务后,session自动关闭   

步骤 1 :

先运行,看到效果,再学习

edit
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 3 :

OpenSession每次都会得到一个新的Session对象

edit
OpenSession每次都会得到一个新的Session对象,所以System.out.println(s1==s2);会打印false
OpenSession每次都会得到一个新的Session对象
package com.how2java.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class TestHibernate { public static void main(String[] args) { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session s1 = sf.openSession(); Session s2 = sf.openSession(); System.out.println(s1==s2); s1.close(); s2.close(); sf.close(); } }
package com.how2java.test;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class TestHibernate {
    public static void main(String[] args) {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
 
        Session s1 = sf.openSession();
        Session s2 = sf.openSession();
        
        System.out.println(s1==s2);
        
        s1.close();
        s2.close();
        sf.close();
    }
}
步骤 4 :

相同线程的getCurrentSession

edit
如果是同一个线程(本例是在主线程里),每次获取的都是相同的Session
相同线程的getCurrentSession
package com.how2java.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class TestHibernate { public static void main(String[] args) { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session s1 = sf.getCurrentSession(); Session s2 = sf.getCurrentSession(); System.out.println(s1 == s2); s1.close(); // s2.close(); sf.close(); } }
package com.how2java.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TestHibernate {
	public static void main(String[] args) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();

		Session s1 = sf.getCurrentSession();
		Session s2 = sf.getCurrentSession();

		System.out.println(s1 == s2);

		s1.close();
//		s2.close();
		sf.close();
	}
}
步骤 5 :

不同线程的getCurrentSession

edit
如果是不同线程,每次获取的都是不同的Session
不同线程的getCurrentSession
package com.how2java.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class TestHibernate { static Session s1; static Session s2; public static void main(String[] args) throws InterruptedException { final SessionFactory sf = new Configuration().configure().buildSessionFactory(); Thread t1 = new Thread() { public void run() { s1 = sf.getCurrentSession(); } }; t1.start(); Thread t2 = new Thread() { public void run() { s2 = sf.getCurrentSession(); } }; t2.start(); t1.join(); t2.join(); System.out.println(s1 == s2); } }
步骤 6 :

openSession查询时候不需要事务

edit
如果是做增加,修改,删除是必须放在事务里进行的。 但是如果是查询或者get,那么对于openSession而言就不需要放在事务中进行
package com.how2java.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.how2java.pojo.Product; public class TestHibernate { static Session s1; static Session s2; public static void main(String[] args) throws InterruptedException { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session s1 = sf.openSession(); s1.get(Product.class, 5); s1.close(); sf.close(); } }
步骤 7 :

getCurrentSession所有操作都必须放在事务中

edit
对于getCurrentSession而言所有操作都必须放在事务中,甚至于查询和get都需要事务。
16行的get没有放在事务中,就会导致异常产生
getCurrentSession所有操作都必须放在事务中
package com.how2java.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.how2java.pojo.Product; public class TestHibernate { public static void main(String[] args) throws InterruptedException { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session s1 = sf.getCurrentSession(); s1.get(Product.class, 5); s1.close(); sf.close(); } }
步骤 8 :

getCurrentSession在提交事务后,session自动关闭

edit
22行,在事务关闭后,试图关闭session,就会报session已经关闭的异常
getCurrentSession在提交事务后,session自动关闭
package com.how2java.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.how2java.pojo.Product; public class TestHibernate { public static void main(String[] args) throws InterruptedException { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session s1 = sf.getCurrentSession(); s1.beginTransaction(); s1.get(Product.class, 5); s1.getTransaction().commit(); s1.close(); sf.close(); } }


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2023-07-31 大概率是bug了吧。。
四方1




用debug断点调试,算的结果也是true,但是console出来的也的确是false
加载中

							

							





回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2018-07-05 楼下问题还是想不通
夜神1997




getCurrentSession()得到的Session就没有eq结果为true的吗
加载中

							

							


1 个答案

Memory_123
答案时间:2019-01-15
equal比较的是内容是否一致,==直接比较的是内存地址是否一致。上述的开启方式查询的是是否开启了新的session。比较的是内存地址



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2017-10-10 为什么getCurrentSession得到的两个session用eq?的得到的结果不是true?
2017-10-09 为什么getCurrentSession的查询和get也要放在事务中呢?




提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 框架-Hibernate-两种Session方式 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图