LazyInitializationException with @Transactional annotation in MessageConverter
I have a REST service exposed through Spring-MVC. I have a specific method that displays correctly and is called via an HTTP call. Spring app contains HibernateTransactionManager
and transactions are configured using @Transactional
-nnotes. I annotated the method as follows:
@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 mine org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
bound in my application context. Is the annotation @Transactional
valid for MessageConverter
?
a source to share
Your converter class is obviously reading a field configured for lazy collection in your Hibernate config.
Two possible solutions:
- Expand your transactional method to include the converter method.
- Edit your Hibernate config to get the field responsible for
LazyInitializationException
. (This field could be, for example, part of a relationship between two tables in a database.)
a source to share
LazyInitializationException
means your hibernate was Session
closed while trying to read uninitialized data about your entity.
You can fix this:
- or extending the session lifetime (using OpenSessionInView
- pre-initialize the object in your service method using
Hibernate.initialize(entity)
a source to share