0

I am working in Spring-Hibernate application. Flow is as usual: Controller --> Service --> DAO.

I have annotated Service layer class with @Transactional, thereby marking every method transactional in that class.In service class, I made a DAO call to get some domain object and then converting it into DTO/VO object which will be passed to controller.For converting domain object to DTO, I have written another custom static class(class having only static methods) like ObjectMapper which will do this conversion.

Now, domain object has some child object(One to Many) which is lazily loaded. So, when in ObjectMapper, i access that child getter method, an extra database call is issued, which is working fine. What I don't understand is that since ObjectMapper is not transactional, I was expecting some exception to be thrown like Session is closed while making database call to fetch child object from database.I am using getCurrentSession of Session Factory in DAO.

Can someone please explain me this behavior?

4
  • 1
    if the relation is one-to-one or Many-to-One it's by default eagerly fetch Commented Sep 30, 2015 at 7:19
  • It's One to many relation..have updated the same in post Commented Sep 30, 2015 at 7:21
  • Are you sure you don't access to this collection before giving it your ObjectMapper ? Does you ObjectMapper call inside your service ? Commented Sep 30, 2015 at 7:22
  • Yes, I call ObjectMapper inside service, but getter of child object is invoked inside ObjectMapper Commented Sep 30, 2015 at 7:46

2 Answers 2

1

I suppose you either call your ObjectMapper from the transactional Service method (you should) or if not, maybe you are enabled "hibernate.enable_lazy_load_no_trans" which keeps the hibernate session open

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I am calling ObjectMapper from transactional Service method but still should it work?
yes, as long as you call the mapper from the transactional method then the session is open for sure
But somewhere I read that @Transactional doesn't work for method call inside method since Transactional is run by Spring AOP.
0

As long as the call to the static class/method, which transforms your objects, is made in the transactional method of the DAO, the session is still open and will be used for the database calls.

4 Comments

But ObjectCaller is neither Spring bean or @transactional..so how come it is working??
There you go every thing that is called inside a transactional method is transactional. The transaction start before the call of your method and stop after the method return.
But somewhere I read that @Transactional doesn't work for method call inside method since Transactional is run by Spring AOP.
Yes, if there is no open transaction and you want that method call to start a new one, then it will not work. But this is different from your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.