春季交易&休眠:延迟初始化

编程入门 行业动态 更新时间:2024-10-09 02:29:58
本文介绍了春季交易&休眠:延迟初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 从我读过的东西中,我了解到使用事务可以解决休眠的延迟加载问题。会话将在服务层的整个交易过程中可用,而不需要额外的补充。

因此,也许我错误地配置了我的事务管理?我实际上是一个newb当谈到春季和冬眠,但也许你们可以帮助我。

我的配置:

< bean class =org.springframework.orm.hibernate3.LocalSessionFactoryBean id =sessionFactory> < property name =configLocation> < value> classpath:hibernate.cfg.xml< / value> < / property> < / bean> <! - 将分配给DAO的Hibernate模板bean。 - > < bean id =hibernateTemplateclass =org.springframework.orm.hibernate3.HibernateTemplate> < property name =sessionFactory> < ref bean =sessionFactory/> < / property> < / bean> <! - 单个Hibernate SessionFactory的事务管理器(替代给JTA) - > < bean id =transactionManager class =org.springframework.orm.hibernate3.HibernateTransactionManager> < property name =sessionFactory> < ref local =sessionFactory/> < / property> < / bean> < tx:注解驱动的事务管理器=transactionManager/>

我的DAO实现只需要一个 @Repository annotation和一个 Hibernate-template 使用自动装配注入的bean

服务的典型标题实现为: $ @Service @Transactional(readOnly = true) public class LeerlingServiceImpl implements LeerlingService { @Autowired LeerlingDAO leerlingDAO; @Autowired LeerplanDAO leerplanDAO;

使用 @Service(readOnly = false)注解,如果任何东西实际上保存/更新在该特定的方法。

我需要配置别的东西,以确保我可以加载正确的关联我的服务,还是通常由交易处理?

现在我只是对我应该做的事情感到困惑,所以请帮助我: )

解决方案

延迟加载问题和事务并不真正与其他相关。但这是另一回事:) 除了在bean中访问会话之外,您已经做得很好。不知道你将如何去做这件事。标准解决方案(在Spring 2.x中,不确定关于3.x,还没有看过)是使用HibernateDaoSupport作为类的基类,你将有权访问会话。但个人来说,这看起来有点狡猾,因为它增加了对Spring特定类的依赖。更好的方法是将会话注入到bean中。要做到这一点,你需要声明会话bean的定义类似于那个:

< bean name =hibernateSessionclass =org.springframework.orm.hibernate3.SessionFactoryUtilsfactory-method =getSession$ b $ scope =prototype> < constructor-arg index =0ref =hibernateSessionFactory/> < constructor-arg index =1value =false/> < aop:scoped-proxy /> < / bean>

然后使用它。

以下是详细信息:

stas-blogspot.blogspot/2009/10/hibernate-spring-in-standalone.html

From what I've read so far I had the understanding that using transactions would be the solution to hibernate's lazy loading problems. The session would be available during the whole transaction in the service layer without further adue.

So maybe I misconfigured my transaction management? I'm actually a newb when it comes to spring and hibernate, but maybe you guys could help me out.

My configuration:

<bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- Hibernate Template bean that will be assigned to DAOs. --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager" />

My DAO implementation would simply have a @Repository annotation and a Hibernate-template bean injected using autowiring.

A typical header of a service Implementation would be:

@Service @Transactional(readOnly=true) public class LeerlingServiceImpl implements LeerlingService { @Autowired LeerlingDAO leerlingDAO; @Autowired LeerplanDAO leerplanDAO;

With a @Service(readOnly=false) annotation if anything is actually saved/updated in that particular method.

Do I need to configure something else to make sure that I can load the right associations in my Service, or is this normally handled by transactions?

Right now I am just a bit confused of what I should actually do, so please help me:)

解决方案

Lazy-loading problems and transactions are not really related one to other. But that's another story :) You've done all well, apart from the access to session in your beans. No sure how you are going to do this. The standard solution (in spring 2.x, not sure about 3.x, haven't looked yet) is to use HibernateDaoSupport as base class for classes were you are going to have an access to session. But personally that looks a little dodgy to me, because adds dependency on Spring-specific classes. Better way is to inject session into your beans. To do this you need to declare your session bean with definition similar to that one:

<bean name="hibernateSession" class="org.springframework.orm.hibernate3.SessionFactoryUtils" factory-method="getSession" scope="prototype"> <constructor-arg index="0" ref="hibernateSessionFactory"/> <constructor-arg index="1" value="false"/> <aop:scoped-proxy/> </bean>

and then just use it.

Here are details:

stas-blogspot.blogspot/2009/10/hibernate-spring-in-standalone.html

更多推荐

春季交易&amp;休眠:延迟初始化

本文发布于:2023-11-12 01:58:23,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   春季   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!