views:

110

answers:

2

Hi,

I have a REST-service exposed through Spring-MVC. I have a particular method which is correctly mapped and called through a HTTP-call. My Spring application contains the HibernateTransactionManager and transactions are configured through @Transactional-annotations. I annotated the method like this:

@Transactional(readOnly = true)
@Override
@RequestMapping(value = "/start", method = RequestMethod.GET)
@ResponseBody
public List<SomeObject> start(....)

Whenever I call the HTTP-method I a org.hibernate.LazyInitializationException from my org.springframework.http.converter.json.MappingJacksonHttpMessageConverter which is bound in my application context. Is the @Transactional annotation valid for the MessageConverter as well?

+1  A: 

LazyInitializationException means that your hibernate Session is closed at the time you try to read uninitialized data on your entity.

You can fix this by:

  • either extending the lifetime of the session (using OpenSessionInView
  • pre-initialize the entity in your service method, using Hibernate.initialize(entity)
Bozho
+2  A: 

Your converter class is obviously reading a field that's configured to be collected lazily in your Hibernate configuration.

Two possible solutions:

  • Expand your transactional method to include the converter method.
  • Edit your Hibernate configuration to eagerly fetch the field that's responsible of the LazyInitializationException. (This field can for example be a part of a relationship between two tables in the database.)
Espen
I already fixed my problem by doing what was your first proposal. Actually the problem was that another tool that I used for mapping objects, copyed persistbags instead of the real collections...
Karl