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()` 和 `Session.openSession()` 的区别对于优化和正确使用Hibernate至关重要。 首先,`SessionFactory` 是Hibernate中的一个接口,它是一个全局的、线程安全的工厂类,用于...
当我们调用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....
在Java的Hibernate框架中,`getCurrentSession()` 和 `openSession()` 都是用于获取与数据库交互的Session对象,但它们之间存在显著的区别。理解这些差异对于优化数据访问性能和管理事务至关重要。 首先,`...
Session session1 = sessionFactory.openSession(); Transaction tx1 = session1.beginTransaction(); // 加载一个持久化对象 Customer customer = (Customer) session.get(Customer.class, new Long(1)); session...
Session session = factory.openSession(); Serializable id = null; Transaction tran = null; try{ tran = session.beginTransaction(); id = session.save(u); tran.commit(); }catch(Exception exp)...
Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setName("John Doe"); session.save(user); ...
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); MyEntity entity = new MyEntity(); // 设置属性... session.save(entity); tx.commit(); session.close(); ``` - ...
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setUsername("testUser"); session.save(user); transaction.commit...
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setUsername("test"); user.setPassword("test123"); session.save(user...
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); MyEntity entity = new MyEntity(); // 设置属性... session.save(entity); tx.commit(); session.close(); ``` *...
Session session = factory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setName("John"); user.setEmail("john@example.com"); session.save(user); ...
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save(book); tx.commit(); session.close(); } @Override public void delete(String ISBN) { ...
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); MyEntity entity = new MyEntity(); // 设置属性值... session.save(entity); transaction.commit(); ...
Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setName("张三"); user.setEmail("zhangsan@example.com")...
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) ...
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setName("John"); user.setEmail("john@example.com"); session....
Session session = sessionFactory.openSession(); SQLQuery query = session.createSQLQuery("SELECT * FROM User"); List results = query.list(); ``` 这种方法可以自由地编写任何有效的SQL,但需要手动映射...