[Java web]-- spring3(6)

本文探讨了Hibernate中延迟加载的异常及解决方法,并介绍了如何通过Spring提供的OpenSessionInViewFilter来实现延迟加载的支持。此外,还讲解了如何利用注解进行SSH(Spring+Struts+Hibernate)框架整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.hibernate   延迟加载异常:在事务外部,初次使用了延迟加载着的属性
  *<class lazy="false"  
   <set  lazy="false"
   <many-to-one lazy="false"
  *DAO  __  Service   强制加载延迟属性(随意使用一下延迟属性)
  *openSessionInView  osiv  spring提供了一个Filter名为:
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  
1>其作用:持有SessionFactory,被此过滤器过滤的请求,会有如下的支持:
     :在事务提交之后仍保持session的open的,直到当前请求的View(视图,jsp)渲染完毕,此时,
 :才会将session关闭.
 :此过滤器会到spring工厂中查找id为sessionFactory的bean
    2>使用流程:在web.xml中配置如下的过滤器,过滤需要使用延迟属性的请求
<!-- OpenSessioInViewFilter
   注意:所有过滤器必选在struts2的前端控制器之前配置
   -->
 <filter>
<filter-name>osiv35</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <filter-mapping>
<filter-name>osiv35</filter-name>
<url-pattern>/ssh35/ssh_queryUserById</url-pattern>
<url-pattern>/ssh35/ssh_queryUserByName</url-pattern>
 </filter-mapping>
 *此Filter中需要的SessionFactory会默认用[sessionFactory]到spring工厂中查找
 *但如果工厂中的Sessionfactory没有采用此ID,则需要告知此Filter:
   <filter>
<filter-name>osiv35</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory35</param-value>
</init-param>
</filter>
============================================================================================
2.基于注解的ssh整合
  *@Component  作用将当前类纳入工厂
   @Scope(value="prototype")  非单例创建模式
   public class UserAction {
  *注入
    @Component
@Scope(value="prototype")
public class UserAction {
@Autowired  在工厂中寻找同类型的bean 自动注入
private UserService userService;
  *事务
@Component
//作用在类中的各个方法中
@Transactional(isolation=Isolation.READ_COMMITTED,rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
public class UserServiceImpl implements UserService{
....
//单独为某个方法指定事务属性
@Transactional(readOnly=true,isolation=Isolation.READ_COMMITTED,rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
public void queryUserPage(Page page) throws Exception {
userDAO.queryUserPage(page);
}
}
  *注意:注解使用,需要添加配置:
     <!-- 注解的所在包路径 -->
<!-- <context:component-scan base-package="com.c35.action,com.c35.dao,com.c35.service"></context:component-scan>
-->
<context:component-scan base-package="com.c35"></context:component-scan>
<!-- 事务管理器 -->
<bean id="txM" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory35"></property>
</bean>
<!-- 为事务注解指定事务管理器 -->
<tx:annotation-driven transaction-manager="txM"/>
============================================================================================






 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oo寻梦in记

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值