私は他に何を試すべきかわかりません、ジャクソン2に更新しました、レイジーに変更し、@ JsonIgnoreを使用しました...しかし何もうまくいきませんでした。何ができるのでしょうか?
環境
[〜#〜] error [〜#〜] Estado HTTP 500-JSONを書き込めませんでした:ロールのコレクションの遅延初期化に失敗しました:com.javalabs.web.dao.Task.actions、セッションなしまたはセッションが閉じられました(参照チェーンを介して: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です:ロールのコレクションの遅延初期化に失敗しました:com.javalabs.web.dao.Task.actions、セッションまたはセッションは閉じられません(参照チェーン:Java.util.HashMapを使用) ["actions"]-> Java.util.ArrayList [0]-> com.javalabs.web.dao.TaskAction ["task"]-> com.javalabs.web.dao.Task ["actions"])
エラートレース
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)
[〜#〜] model [〜#〜]
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;
...
}
ser.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();
}
}
まず第一に、ここではUser.Javaはまったく必要ありません。それには何もありません。この問題はTask.actionsに関連しています。 TaskActionのコレクションをロードします。TaskActionにはTaskがあり、このTaskには初期化できないTaskActionのコレクションがあります。以下に@JsonIgnore
を追加できます。
private List<TaskAction> actions = new ArrayList<TaskAction>(); //Task class
または:
private Task task; //TaskAction class
Task.JavaのアクションのコレクションでFetchType.LAZY
をFetchType.EAGER
に変更することもできますが、動作するかどうかはわかりません(循環参照)
Hibernate Sessionオブジェクトの外部でオブジェクトにアクセスしているため、モデルの各@OneToManyアノテーションの前にアノテーション@JsonIgnoreを追加する必要があります。
エンティティクラスの各@OneToManyアノテーションの前にアノテーション@JsonIgnoreを追加してください
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "domainDataId")
ありがとう、Shahid Hussain Abbasi
上記と同じ例外を示す問題があり、「セッションなし」について不平を言っていましたが、問題はIDではなくDTOで参照されているエンティティが原因であることがわかりました。したがって、上記の問題が発生した場合に備えて、シリアル化の問題が発生していないかどうかを確認することをお勧めします。
私は最近、同様の問題に遭遇しました:ジャクソンバージョン1.9以前(両方向関係)(サイクルを回避)
そして、ソリューションはJackson 2.0にアップグレードし、クラスに次の注釈を追加するようです。
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class,
property = "idTaskAction")
public class TaskAction