栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring,@Transactional和Hibernate延迟加载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring,@Transactional和Hibernate延迟加载

Hibernate最近推出了获取配置文件(除了性能调整之外)对于解决此类问题也是理想的选择。它允许您(在运行时)在不同的加载和初始化策略之间进行选择。

http://docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-
fetching-
profiles

编辑 (添加了有关如何使用拦截器设置获取配置文件的部分):

开始之前:检查获取配置文件实际上是否对您有用。我还没有亲自使用过它们,而是看到它们目前仅限于加入访存。在浪费时间实现和连接拦截器之前,请尝试手动设置获取配置文件,以查看它实际上可以解决您的问题。

有很多方法可以在Spring中设置拦截器(根据偏好),但是最直接的方法是实现MethodInterceptor(请参阅http://static.springsource.org/spring/docs/3.0.x/spring-
framework-reference / html / aop-api.html#aop-api-advice-
around)。让它具有所需的获取配置文件的设置器和Hibernate Session工厂的设置器:

public class FetchProfileInterceptor implements MethodInterceptor {    private SessionFactory sessionFactory;    private String fetchProfile;    ... setters ...    public Object invoke(MethodInvocation invocation) throws Throwable {        Session s = sessionFactory.openSession(); // The transaction interceptor has already opened the session, so this returns it.        s.enableFetchProfile(fetchProfile);        try { return invocation.proceed();        } finally { s.disableFetchProfile(fetchProfile);        }    }}

最后,在Spring配置中启用拦截器。这可以通过几种方式完成,您可能已经具有可以将其添加到的AOP设置。参见http://static.springsource.org/spring/docs/3.0.x/spring-
framework-reference/html/aop.html#aop-
schema。

如果您不熟悉AOP,建议您先尝试使用“旧的”
ProxyFactory方法(http://static.springsource.org/spring/docs/3.0.x/spring-
framework-reference/html/aop-api .html#aop-api-proxying-
intf),因为它更容易理解其工作原理。这是一些示例XML,可以帮助您入门:

<bean id="fetchProfileInterceptor" >  <property name="sessionFactory" ref="sessionFactory"/>  <property name="fetchProfile" ref="gui-profile"/></bean><bean id="businessService" >  <property name="dao" .../>  ...</bean><bean id="serviceForSwinGUI"     >    <property name="proxyInterfaces" value="x.y.z.BusinessServiceInterface/>    <property name="target" ref="businessService"/>    <property name="interceptorNames">        <list> <value>existingTransactionInterceptorBeanName</value> <value>fetchProfileInterceptor</value>        </list>    </property></bean>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401338.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号