Frage

Von dem, was ich bisher gelesen habe ich das Verständnis, dass die Transaktionen mit der Lösung wäre, die faul Ladeprobleme zu überwintern. Die Sitzung würde während der gesamten Transaktion in der Dienstschicht ohne weiteren ADUE zur Verfügung.

Also vielleicht falsch konfigurierte ich mein Transaktionsmanagement? Ich bin eigentlich ein Newb, wenn es um Frühling kommt und überwintern, aber vielleicht euch könnte mir helfen.

Meine Konfiguration:

<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" />

Meine DAO Implementierung würde einfach eine @Repository Anmerkung haben und eine Hibernate-template Bohne mit autowiring injiziert.

Ein typischer Header einer Service-Implementierung wäre:

@Service
@Transactional(readOnly=true)
public class LeerlingServiceImpl implements LeerlingService {

    @Autowired
    LeerlingDAO leerlingDAO;
    @Autowired
    LeerplanDAO leerplanDAO;

Mit einer @Service(readOnly=false) Anmerkung, wenn irgendetwas in diesem speziellen Verfahren tatsächlich gespeichert / aktualisiert wird.

Muß ich configure etwas anderes, um sicherzustellen, dass ich die richtigen Assoziationen in meinem Dienst laden, oder dies in der Regel durch Transaktionen abgewickelt wird?

Im Moment bin ich nur ein bisschen verwirrt von dem, was ich eigentlich tun sollte, bitte so helfen Sie mir:)

War es hilfreich?

Lösung

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:

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

Andere Tipps

I think my understanding of Spring was just bad till now; there was indeed no real management for our session management. Basically what now happened was: you could get data from the DAO, but right after you received it you couldn't even get lazy collections loaded because the session was closed.

Now we are using the hibernate interceptor, which attaches the session to the thread at the beginning of each request and closes it when it ends. This is not always the most ideal solution, but for a school project I wouldn't bother too much.

The other solution seems to be: add AOP in a way that @around is used that the session is only available during a service method call. I think this is the best solution, though, I'm not going to dig that deeply right now for this project. The good thing is that I know it exists.

This article also helped me a lot: http://www.jroller.com/kbaum/entry/orm_lazy_initialization_with_dao

To those interested: here is what I had to add: In Spring MVC 3.0 there is a new feature called mvc:intereceptors which made me type less xml.

<!-- WEB-INF/applicationContext.xml or your own XML config file -->
<mvc:interceptors>
    <bean
        class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
</mvc:interceptors>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top