当Dao层继承了HibernateDaoSupport,使用底层SQL语句,session获取的方法,以及解决关联查询no session的问题
2017-05-28 00:25
916 查看
一、获取session的过程
Session session=this.getSessionFactory().getCurrentSession();
二、在web.xml文件中要配置一个过滤器,不然会报异常Could not obtain transaction-synchronized Session for current thread
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
同时,该配置也可以解决no session问题,所谓no session问题,就是在页面中使用关联查询,即${linkman.coustomer.cname},由于linkman表只有cid,所以会根据cid查询coustomer表得到cname,但是dao层操作完,session会自动关闭,出现no session,所以要配置来延迟关闭session。
解决no session的第二种方法,可以在配置一对多关系时,设置lazsy属性。
如:在实体类categorySecond(一)中的Set<Product> products(多)
<set name="products" lazy="false">
<key column="csid"></key>
<one-to-many class="cn.itcast.store.product.vo.Product"/>
</set>
设置lazy为false,表示延迟session的关闭,这样在页面中就可以使用关联查询${category.categorySeconds},我们查询的category表没有categorySeconds的信息,但他会再次查询数据库,根据cid查询出所属的categorySeconds的信息。这是一方处理no session。
同样,多方的lazy配置
<many-to-one name="categorySecond"lazy="false" class="cn.itcast.store.cagetorysecond.vo.CategorySecond"
column="csid"> </many-to-one>
在页面中使用关联查询,即${categorySecond.category.cname},由于categorySecond表只有cid,所以会根据cid查询category表得到cname,
Session session=this.getSessionFactory().getCurrentSession();
二、在web.xml文件中要配置一个过滤器,不然会报异常Could not obtain transaction-synchronized Session for current thread
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
同时,该配置也可以解决no session问题,所谓no session问题,就是在页面中使用关联查询,即${linkman.coustomer.cname},由于linkman表只有cid,所以会根据cid查询coustomer表得到cname,但是dao层操作完,session会自动关闭,出现no session,所以要配置来延迟关闭session。
解决no session的第二种方法,可以在配置一对多关系时,设置lazsy属性。
如:在实体类categorySecond(一)中的Set<Product> products(多)
<set name="products" lazy="false">
<key column="csid"></key>
<one-to-many class="cn.itcast.store.product.vo.Product"/>
</set>
设置lazy为false,表示延迟session的关闭,这样在页面中就可以使用关联查询${category.categorySeconds},我们查询的category表没有categorySeconds的信息,但他会再次查询数据库,根据cid查询出所属的categorySeconds的信息。这是一方处理no session。
同样,多方的lazy配置
<many-to-one name="categorySecond"lazy="false" class="cn.itcast.store.cagetorysecond.vo.CategorySecond"
column="csid"> </many-to-one>
在页面中使用关联查询,即${categorySecond.category.cname},由于categorySecond表只有cid,所以会根据cid查询category表得到cname,
相关文章推荐
- 继承HibernateDaoSupport后,获得session 并使用session操作sql的方法
- 使用 Hibernate 在 JPA 中 调用 generateSchema 会出现执行两次 SQL 语句问题的解决方法
- 解决mybatis继承SqlSessionDaoSupport方式操作数据库不打印sql日志的问题。
- hibernate使用原生sql查询Hibernate原生SQL多表查询字段名重复问题以及解决方法
- "执行SQL语句时出现问题操作必须使用一个可更新的查询"错误的解决方法
- 使用Hibernate的session执行任意SQL语句以及整个事务
- 解决继承HibernateDaoSupport用getHibernateTemplate()无法更新的问题
- 使用Hibernate的session执行任意SQL语句以及整个事务
- 继承HibernateDaoSupport后怎样获取session&&注入sessionFactory
- <?>关于spring里HibernateDaoSupport的关于session的获取方法
- 解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题
- 解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题
- 使用CInternetSession获取utf-8页面中文乱码问题解决方法
- 使用CInternetSession获取utf-8页面中文乱码问题解决方法
- jboss7.1.1下使用log4j输出mybatis3.2.2的SQL语句问题与解决方法
- DAO也可以通过继承SqlSessionDaoSupport获取SqlSession对象
- Hibernate使用中sessionFactory的获取以及问题解决
- hibernate 使用延迟加载产生的问题以及对应的解决方法
- Mysql中文乱码以及导出为sql语句和Excel问题解决方法[图文]
- HibernateDaoSupport的getHibernateTemplate.find()方法带参查询返回结果是nul问题解决