I'm unsure of which of the above to use in our DAOs (which extend HibernateDAOSupport).
The SpringFramework API doc on HibernateTemplate.getSession() is thus:
protected Session getSession()
Return a Session for use by this template.
Returns a new Session in case of alwaysUseNewSession" (using the same JDBC Connection as a transactional Session, if applicable), a pre-bound Session in case of "allowCreate" turned off, and a pre-bound or new Session else (new only if no transactional or otherwise pre-bound Session exists).
The SpringFramework API doc on getSession() is thus:
protected final Session getSession()
throws DataAccessResourceFailureException,
IllegalStateException
Get a Hibernate Session, either from the current transaction or a new one. The latter is only allowed if the "allowCreate" setting of this bean's HibernateTemplate is true.
Note that this is not meant to be invoked from HibernateTemplate code but rather just in plain Hibernate code. Either rely on a thread-bound Session (via HibernateInterceptor), or use it in combination with releaseSession.
In general, it is recommended to use HibernateTemplate, either with the provided convenience operations or with a custom HibernateCallback that provides you with a Session to work on. HibernateTemplate will care for all resource management and for proper exception conversion.
分享到:
相关推荐
Query query = getHibernateTemplate().getSession().createQuery(hql); query.setFirstResult((currentPage - 1) * lineSize); query.setMaxResults(lineSize); List<Person> personList = query.list(); ``` 总的...
- 保存:`getSession().save()`, `getSession().update()`, `getSession().delete()` - Query的使用: - 可以设置参数,类似PreparedStatement - 判断结果,通过`list.size()`,如有值则通过`list.get(0)`获取 - ...
Query query = getHibernateTemplate().getSession().createQuery(queryString); // 设置起始位置 query.setFirstResult(position); // 设置每页显示的数量 query.setMaxResults(length); List list = query...
与直接使用`getSession()`方法相比,`getHibernateTemplate()`具有以下显著特点: 1. **事务管理**:`getHibernateTemplate()`是Spring封装后的接口,它支持声明式事务管理。这意味着你无需手动开启和关闭事务,...
int row = this.getSession().createQuery(hql).setString(0, "小李想").executeUpdate(); ``` 这里的`setString(0, "小李想")`用于设置参数,`0`表示参数的位置,与`?`在HQL中的位置相对应。 2. **HQL删除**: ...
this.getHibernateTemplate().save(user); } @Override public PageModel findAllUser(int offset, int pageSize) { // 获取总记录数 String queryCountHql = "select count(*) from User"; Query query = ...
this.getHibernateTemplate().save(user); } public PageModel findAllUser(int offset, int pageSize) { // 获取总记录数 String queryCountHql = "select count(*) from User"; Query query = getSession...
2. **Session获取**:提供了`getSession()`方法来获取当前线程的`Session`对象,从而避免了手动管理`SessionFactory`和`Session`的过程。 3. **事务管理**:支持自动提交事务或者回滚事务,根据`hibernateTemplate`...
这种方式是通过`getHibernateTemplate().getSessionFactory().openSession()`来获得一个新的Session,然后通过该Session获取连接。 ```java // 方式一 Session session = this.getHibernateTemplate()....
Session session = this.getHibernateTemplate().getSessionFactory().openSession(); Transaction tran = session.beginTransaction(); try { session.save(userManager); tran.commit(); } catch ...
- 通过`this.getHibernateTemplate().getSessionFactory().openSession();` - 通过`this.getSession();` 这两种方式获取Session后,同样需要使用`releaseSession(session)`释放资源。务必记住,多次查询而未释放...
3. `getSession()`: 提供对当前事务上下文中的Session的访问,这对于执行HQL或SQL查询非常有用。 接下来,我们谈谈`@Autowired`。这是Spring框架的一个关键注解,用于实现依赖注入(Dependency Injection, DI)。DI...
为了进行数据库操作,通常会创建一个继承自`HibernateDaoSupport`的DAO类,这样可以直接使用`getSession()`方法进行操作。例如: ```java public class Dao extends HibernateDaoSupport { public List selectList...