Je ne sais pas quoi essayer d'autre. J'ai mis à jour Jackson 2, changé en Lazy, utilisé @JsonIgnore ... mais rien n'a fonctionné. Une idée de ce que ça peut être?
Environnement
ERREUR État HTTP 500 - Impossible d'écrire en JSON: impossible d'initialiser paresseusement une collection de rôles: com.javalabs.web.dao.Task.actions; aucune session ou session n'a été fermée (via la chaîne de références: Java.util.HashMap ["actions" ] -> Java.util.ArrayList [0] -> com.javalabs.web.dao.TaskAction ["tâche"] -> com.javalabs.web.dao.Task ["actions"]); l'exception imbriquée est com.fasterxml.jackson.databind.JsonMappingException: échec de l'initialisation paresseuse d'une collection de rôles: com.javalabs.web.dao.Task.actions; aucune session ou session n'a été fermée (via la chaîne de référence: Java.util.HashMap ["actions"] -> Java.util.ArrayList [0] -> com.javalabs.web.dao.TaskAction ["tâche"] -> com.javalabs.web.dao.Task ["actions"])
Trace d'erreur
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.javalabs.web.dao.Task.actions, no session or session was closed (through reference chain: Java.util.HashMap["actions"]->Java.util.ArrayList[0]->com.javalabs.web.dao.TaskAction["task"]->com.javalabs.web.dao.Task["actions"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.javalabs.web.dao.Task.actions, no session or session was closed (through reference chain: Java.util.HashMap["actions"]->Java.util.ArrayList[0]->com.javalabs.web.dao.TaskAction["task"]->com.javalabs.web.dao.Task["actions"])
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.Java:207)
org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.Java:179)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.Java:148)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.Java:90)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.Java:189)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.Java:69)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:122)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.Java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.Java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.Java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:154)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.Java:199)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:110)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.Java:50)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.Java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:343)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:260)
causa raíz
com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.javalabs.web.dao.Task.actions, no session or session was closed (through reference chain: Java.util.HashMap["actions"]->Java.util.ArrayList[0]->com.javalabs.web.dao.TaskAction["task"]->com.javalabs.web.dao.Task["actions"])
com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.Java:232)
com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.Java:197)
com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.Java:184)
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.Java:605)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.Java:142)
com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.Java:569)
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.Java:597)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.Java:142)
com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.Java:100)
com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.Java:21)
com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.Java:186)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.Java:394)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.Java:315)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.Java:27)
com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.Java:118)
com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.Java:1819)
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.Java:204)
org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.Java:179)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.Java:148)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.Java:90)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.Java:189)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.Java:69)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:122)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.Java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.Java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.Java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:154)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.Java:199)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:110)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.Java:50)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.Java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:343)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:260)
causa raíz
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javalabs.web.dao.Task.actions, no session or session was closed
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.Java:383)
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.Java:375)
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.Java:368)
org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.Java:111)
org.hibernate.collection.PersistentBag.iterator(PersistentBag.Java:272)
com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.Java:90)
com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.Java:23)
com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.Java:186)
com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.Java:569)
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.Java:597)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.Java:142)
com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.Java:569)
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.Java:597)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.Java:142)
com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.Java:100)
com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.Java:21)
com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.Java:186)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.Java:394)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.Java:315)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.Java:27)
com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.Java:118)
com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.Java:1819)
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.Java:204)
org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.Java:179)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.Java:148)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.Java:90)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.Java:189)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.Java:69)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:122)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.Java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.Java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.Java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:154)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.Java:199)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:110)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.Java:50)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.Java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:343)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:260)
MODÈLE
TaskController.Java
@Controller
public class TaskController {
...
@RequestMapping(value = "/getactions/{id}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody Map<String, Object> getActions(Principal principal,
@PathVariable Long id) {
logger.info("Task controller get actions...");
List<TaskAction> actions = null;
if (principal == null) {
actions = new ArrayList<TaskAction>();
} else {
actions = taskActionService.getAllTaskActions(id);
}
Map<String, Object> data = new HashMap<String, Object>();
data.put("actions", actions);
data.put("number", actions.size());
return data;
}
}
Task.Java
@Entity
@Table(name = "t_task")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idTask")
private long idTask;
@Size(min = 5, max = 100)
@Column(name = "taskname")
private String taskname;
...
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "task", orphanRemoval = true)
private List<TaskAction> actions = new ArrayList<TaskAction>();
@Column(name = "timestamp")
private Timestamp timestamp;
...
}
User.Java @ Entity @ Table (name = "t_user") Public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "idUser")
private long idUser;
...
@ManyToMany(cascade = { CascadeType.ALL })
@JsonIgnore
@JoinTable(name = "r_userrole", joinColumns = { @JoinColumn(name = "idUser") }, inverseJoinColumns = { @JoinColumn(name = "idRole") })
private Set<Role> roles = new HashSet<Role>();
...}
TaskAction.Java
@Entity
@Table(name = "t_taskaction")
public class TaskAction {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idTaskAction")
private long idTaskAction;
@ManyToOne
@JoinColumn(name = "idTask", nullable = false)
private Task task;
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idUser", nullable = false)
private User user;
@Column(name = "timestamp")
private Date timestamp;
...
}
TaskActionDao.Java
@Repository
@Transactional
@Component("taskActionDao")
public class TaskActionDao {
@Autowired
private SessionFactory sessionFactory;
public Session session() {
return sessionFactory.getCurrentSession();
}
...
@SuppressWarnings("unchecked")
public List<TaskAction> getAllTaskActions() {
Query q = session().createQuery("from TaskAction");
List<TaskAction> la = q.list();
//createQuery("from TaskAction").list();
System.out.println(">>>>>>>>>>getAllActions>>>>>>>>>>> " + la.size());
return la;
}
@SuppressWarnings("unchecked")
public List<TaskAction> getAllTaskActions(long idTask) {
Criteria crit = session().createCriteria(TaskAction.class);
crit.add(Restrictions.eq("task.idTask", idTask));
System.out.println(">>>>>>>>getAllActions(id)>>>>>>>>>>>>> " + crit.list().size());
return crit.list();
}
}
Tout d'abord, nous n'avons pas du tout besoin de User.Java. Cela n'a rien à voir. Le problème est lié à Task.actions. Vous chargez la collection de TaskAction, TaskAction a Task et cette tâche a une collection de TaskAction qui ne peut pas être initialisée. Vous pouvez ajouter @JsonIgnore
à:
private List<TaskAction> actions = new ArrayList<TaskAction>(); //Task class
ou pour:
private Task task; //TaskAction class
Vous pouvez également essayer de remplacer FetchType.LAZY
par FetchType.EAGER
pour une collection d'actions dans Task.Java, mais je ne suis pas sûr que cela va fonctionner (références circulaires).
Puisque vous accédez à votre objet en dehors de l’objet Hibernate Session, vous devriez donc Ajouter l’annotation @JsonIgnore avant chaque annotation @OneToMany dans votre modèle.
Veuillez ajouter une annotation @JsonIgnore avant chaque annotation @OneToMany dans votre classe d'entités Ma solution
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "domainDataId")
Merci à Shahid Hussain Abbasi
J'avais un problème montrant la même exception que ci-dessus, à propos de la «session non planifiée», mais je trouvais que le problème était dû au fait que l'entité était référencée dans le DTO plutôt que son ID. Donc, juste au cas où le problème ci-dessus surviendrait, je suggérerai de vérifier si aucun problème de sérialisation n’a été causé.
J'ai récemment rencontré un problème similaire: Jackson version 1.9 et antérieure avec relations bidirectionnelles (évitant les cycles)
Et la solution semble passer à Jackson 2.0 et ajouter aux classes l'annotation suivante:
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class,
property = "idTaskAction")
public class TaskAction