为了账号安全,请及时绑定邮箱和手机立即绑定

两种方式测试出来的hashCode是一样的,什么原因,是新版本的差异问题吗?

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;

public class SessionTest {

    @Test
    public void testOpenSession() {
        Configuration config = new Configuration().configure(); // 获得配置对象
        // 获取服务注册对象
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties())
                .configure().build();
        // 获得sessionFactory对象
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // 获得session对象
        Session session1 = sessionFactory.openSession();
        Session session2 = sessionFactory.openSession();
        System.out.println(session1==session2); //false
        
        /*
        if (session != null) {
            System.out.println("session创建成功!");
        } else {
            System.out.println("session创建失败!");
        }*/
    }
    
    @Test
    public void testSaveStudentsWithOpenSession() {
        Configuration config = new Configuration().configure(); // 获得配置对象
        // 获取服务注册对象
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties())
                .configure().build();
        // 获得sessionFactory对象
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // 获得session对象
        Session session1 = sessionFactory.openSession();
        //开启事务
        Transaction transaction = session1.beginTransaction();
        //生成一个学生对象
        Students s = new Students(1, "张三", "男", new Date(), "北京");
        session1.doWork(new Work() {

            @Override
            public void execute(Connection connection) throws SQLException {
                // TODO Auto-generated method stub
                System.out.println("connection hashCode:"+connection.hashCode());
            }
            
        });
        session1.save(s);
        //session1.close();
        transaction.commit(); //提交事务
        
        Session session2 = sessionFactory.openSession();
        //开启事务
        transaction = session2.beginTransaction();
        //生成一个学生对象
        s = new Students(2, "李四", "男", new Date(), "上海");
        session2.doWork(new Work() {

            @Override
            public void execute(Connection connection) throws SQLException {
                // TODO Auto-generated method stub
                System.out.println("connection hashCode:"+connection.hashCode());
            }
            
        });
        session2.save(s);
        transaction.commit();
        
    }
    
    @Test
    public void testSaveStudentsWithGetCurrentSession() {
        Configuration config = new Configuration().configure(); // 获得配置对象
        // 获取服务注册对象
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties())
                .configure().build();
        // 获得sessionFactory对象
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // 获得session对象
        Session session1 = sessionFactory.getCurrentSession();
        //开启事务
        Transaction transaction = session1.beginTransaction();
        //生成一个学生对象
        Students s = new Students(1, "张三", "男", new Date(), "北京");
        session1.doWork(new Work() {

            @Override
            public void execute(Connection connection) throws SQLException {
                // TODO Auto-generated method stub
                System.out.println("connection hashCode:"+connection.hashCode());
            }
            
        });
        session1.save(s);
        //session1.close();
        transaction.commit(); //提交事务
        
        Session session2 = sessionFactory.getCurrentSession();
        //开启事务
        transaction = session2.beginTransaction();
        //生成一个学生对象
        s = new Students(2, "李四", "男", new Date(), "上海");
        session2.doWork(new Work() {

            @Override
            public void execute(Connection connection) throws SQLException {
                // TODO Auto-generated method stub
                System.out.println("connection hashCode:"+connection.hashCode());
            }
            
        });
        session2.save(s);
        transaction.commit();
    }
    

    @Test
    public void testGetCurrentSession() {
        Configuration config = new Configuration().configure(); // 获得配置对象
        // 获取服务注册对象
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties())
                .configure().build();
        // 获得sessionFactory对象
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // 获得session对象
        Session session1 = sessionFactory.getCurrentSession();
        Session session2 = sessionFactory.getCurrentSession();
        
        System.out.println(session1==session2); //true
        
        /*
        if (session != null) {
            System.out.println("session创建成功!");
        } else {
            System.out.println("session创建失败!");
        }*/
    }
}


下面是cfg.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
      <property name="connection.username">root</property>
      <property name="connection.password">root</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
      <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
      
      <property name="show_sql">true</property>
      <property name="format_sql">true</property>
      <property name="hibernate.default_schema">hibernate</property>
      <property name="hbm2ddl.auto">create</property>
      <property name="hibernate.current_session_context_class">thread</property>
      
      <mapping resource="Students.hbm.xml"/>
      
    </session-factory>
</hibernate-configuration>


正在回答

1 回答

通过getCurrentSession获得的是当前的对话,hash code是一样的

0 回复 有任何疑惑可以回复我~
#1

慕雪1014944 提问者

非常感谢!
2017-09-17 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

两种方式测试出来的hashCode是一样的,什么原因,是新版本的差异问题吗?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信