0

First I use Sping 3.1.1 Transactional

<bean id="transactionManagerHLDC01" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactoryHLDC01" />
    <qualifier value="hldc01"/>
</bean>

and hibernate 4.2.3

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>

Also I have Main entityt (ME) with

@OneToMany(fetch=FetchType.LAZY, mappedBy="host") // <- field in DovecotEmailDomain
@Fetch(FetchMode.SELECT)
private Set<DovecotEmailDomain> domains; 

and child entity (CE)

when I invoke ME.getDomains() It starts Hibernate activitites but it is not within already closed TRANSACTION.

The question IS :

HOW I can cover Hibernate activities with existing Transaction in HEBERNATE4 + SPRING 3.1 ?

alexbt
  • 16,415
  • 6
  • 78
  • 87
Andrew Niken
  • 606
  • 2
  • 8
  • 18
  • Are you calling ME.getDomains() inside a jsp? What's the error you are receiving? How are you handling your transactions opening ? – Emanuele Ivaldi Aug 21 '13 at 17:34
  • Hi Emanuele, 1. When I do not use Hibernate.initialize( ME.getDomains() ) I receives error : Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ru.hostlux.emailmanager.entity.hldovecot .DovecotHost.domains, could not initialize proxy - no Session – Andrew Niken Aug 21 '13 at 21:41
  • 2. When I use Hibernate.initialize(...) the error is : Exception in thread "main" org.hibernate.HibernateException: collection is not associated with any session – Andrew Niken Aug 21 '13 at 21:45
  • 3. I have DAO & service classes with Interfaces, they both use @Transactional per class. – Andrew Niken Aug 21 '13 at 21:46
  • I run simple app ( static main() { ... } ) as test. There is not problem with DAO and Service methods. They both are in @Transactional. But Lasy initialising (in Hibernate4) not covered Spring Transactinal. Do you have any idea how extends current or create new Transaction for Lazy Initialisation ? (At the moment i found only such solution and try to release it : http://9mmedia.com/blog/?p=272 ) – Andrew Niken Aug 21 '13 at 21:48
  • Also I tried to apply @Transactional to method in test class which invoke LAZY init, but nothing changed. – Andrew Niken Aug 21 '13 at 21:51
  • From the exception you are getting seems like there is not a session you are bound to when you are accessing the lazy init list, I think you are already enabling the @Transactional annotation using ``, you should see in the console when a session is opened or closed if the logger if configured well, give it a look – Emanuele Ivaldi Aug 21 '13 at 22:24

0 Answers0