I'm developing a REST API with Jersey, Spring and Hibernate. I have this endpoint:
@Path("incidences")
public class IncidencesResource {
@InjectParam
IncidencesService incidencesService;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Incidence> getIncidences(
@QueryParam("lastupdate") String lastUpdate) {
return incidencesService.getIncidences();
}
and the service:
@Service
public class IncidencesService {
@Autowired
IncidencesDAO incidencesDAO;
@Transactional
public List<Incidence> getIncidences() {
List<Incidence> incidencias = incidencesDao.getIncidences();
return incidences;
}
}
But I get a org.hibernate.LazyInitializationException
error:
Failed to lazily initialize a collection of role: com.api.blabla.Incidence.questions, no session or session was closed
questions
is a @OneToMany
property of Incidence
.
If I put @Transactional
annotation in the endpoint method, instead of in the service, it works properly. But I understand that @Transactional has to be placed at service level.
Thanks.