`
gogototo
  • 浏览: 35396 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Difference between Session.openSession() & Session.getCurrentSession()

阅读更多
getSession gives you a Hibernate session, and associates that session with the current thread. Then, if you call getSession again, you get the session that was originally created. This is good. Some people don't like binding the Hibernate Session to the current thread, and they don't like the ease of getting the current Session back with getSession. Instead, these people use openSession, which creates a new Session and asks the DEVELOPER to manage where the session goes. I gues you could put it in a list or a cache, or whatever, but YOU must manage it, as though you were creating your own database connection pool or something. getSession is usually sufficient. openSession provides and facilitates a greater level of management of where the session is stored and managed. It's certainly an advanced option, but one that does indeed fit the need of very clever developers who are doing some nifty things with the session. Some people who are much better looking than me, not to mention being much more intelligent, discussed just this topic in the following JavaRanch thread: What is the difference between the Hibernate Sessi...ession and getSession methods?
分享到:
评论

相关推荐

    SessionFactory.getCurrentSession与openSession的区别

    理解 `SessionFactory.getCurrentSession()` 和 `Session.openSession()` 的区别对于优化和正确使用Hibernate至关重要。 首先,`SessionFactory` 是Hibernate中的一个接口,它是一个全局的、线程安全的工厂类,用于...

    新Hibernate SessionFactory().getCurrentSession()猫腻

    当我们调用SessionFactory().getCurrentSession()时,Hibernate会为我们提供一个已存在的或者新创建的Session实例,这个行为与直接调用SessionFactory.openSession()有所不同。`getCurrentSession()`方法旨在支持...

    用户管理系统

    Session session=sessionFactory.openSession(); String hql="from User as u where u.username=? and u.userpass=? and u.userright=?"; Query query=session.createQuery(hql) ; query.setString(0, u....

    getCurrentSession 与 openSession() 的区别

    在Java的Hibernate框架中,`getCurrentSession()` 和 `openSession()` 都是用于获取与数据库交互的Session对象,但它们之间存在显著的区别。理解这些差异对于优化数据访问性能和管理事务至关重要。 首先,`...

    hibernate session.doc

    Session session1 = sessionFactory.openSession(); Transaction tx1 = session1.beginTransaction(); // 加载一个持久化对象 Customer customer = (Customer) session.get(Customer.class, new Long(1)); session...

    ssh用户管理系统

    Session session = factory.openSession(); Serializable id = null; Transaction tran = null; try{ tran = session.beginTransaction(); id = session.save(u); tran.commit(); }catch(Exception exp)...

    hibernate框架下的增删改查

    Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setName("John Doe"); session.save(user); ...

    hibernate示例

    Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); MyEntity entity = new MyEntity(); // 设置属性... session.save(entity); tx.commit(); session.close(); ``` - ...

    Hibernate-CRUD自用基础模板

    Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setUsername("testUser"); session.save(user); transaction.commit...

    Hibernate Part 2:单表CRUD

    Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setUsername("test"); user.setPassword("test123"); session.save(user...

    HibernateSessionFactory 代码

    Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); MyEntity entity = new MyEntity(); // 设置属性... session.save(entity); tx.commit(); session.close(); ``` *...

    hibernateCRUD

    Session session = factory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setName("John"); user.setEmail("john@example.com"); session.save(user); ...

    javaee试卷.pdf

    Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save(book); tx.commit(); session.close(); } @Override public void delete(String ISBN) { ...

    Hibernate入门示例.doc

    Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); MyEntity entity = new MyEntity(); // 设置属性值... session.save(entity); transaction.commit(); ...

    hibernate增删改查代码

    Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setName("张三"); user.setEmail("zhangsan@example.com")...

    2022年Hibernate下数据批量处理Java教程.docx

    Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for (int i=0; i; i++) { Customer customer = new Customer(...); session.save(customer); if (i % 20 == 0) ...

    hibernate连接数据[Mysql]的代码实例

    Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setName("John"); user.setEmail("john@example.com"); session....

    hibernate 执行原生sql的几种方式

    Session session = sessionFactory.openSession(); SQLQuery query = session.createSQLQuery("SELECT * FROM User"); List results = query.list(); ``` 这种方法可以自由地编写任何有效的SQL,但需要手动映射...

Global site tag (gtag.js) - Google Analytics