I'm using Spring data
which is easy to use but i can't control it because i got error there
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: entity.Qualite.fonctions, could not initialize proxy - no Session
I know FetchType.EAGER
will work but i want keep it lazy. so how can i control the session in spring data
@RequestMapping(value="/loadfonction")
public @ResponseBody
Set<Fonction> loadfonction(Map<String, Object> model, HttpServletRequest request) {
Set<Fonction> fonctions = qualiteRepo.findOne(Integer.valueOf(request.getParameter("idquality"))).getFonctions();
System.out.println(fonctions.size());
return fonctions;
}
I even try @Transactional annotation but it didn't work:
@Transactional
@RequestMapping(value="/loadfonction")
This is a common problem with trying to open a view using the spring mvc framework. The control method closes the session before the view can display it. (Trying to keep the view out of the business logic) To get around it you can use the OpenSessionInViewFilter class.
Here is an article on how to implement it:
http://blog.cloudmate.pl/2010/09/hibernates-open-session-in-view-in.html
In your repository you cannot use query methods to initialize the collection. Instead you should define a query like that to fetch the collection with it. Change your query according to your domain, I can't really figure out how it should look for you.
@Query("SELECT q FROM Qualite q JOIN FETCH q.role WHERE q.fonctionId = (:fonctionId)")
public Qualite findById(@Param("fonctionId") String fonctionId);
You can't. The only way to avoid this problem is to do a query when you want to retrieve the Fonction
objects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.