gpt4 book ai didi

java - Spring MVC, hibernate : Found shared references to collection when retreiving data

转载 作者:行者123 更新时间:2023-11-29 12:04:52 27 4
gpt4 key购买 nike

我正在开发一个 Spring-MVC 应用程序,其中我使用 hibernate 作为 ORM 工具,使用 PostgreSQL 作为数据库。目前,当我通过几种方法从数据库访问数据时遇到问题。一直以来,错误都是一样的。我没有坚持任何东西,而只是请求数据。

对于 3-4 个 DAO 方法,问题总是发生在 getUniqueResult 处。你能帮忙的话,我会很高兴。另外,我检查了答案,但他们只给出了一些场景,它们都不适用,因为我只是查询数据。

错误代码:

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: Found shared references to a collection: com.journaldev.spring.model.Person.canvas1

org.hibernate.HibernateException: Found shared references to a collection: com.journaldev.spring.model.Person.canvas1
com.journaldev.spring.dao.GroupAttachmentsDAOImpl.returnAttachmentCount(GroupAttachmentsDAOImpl.java:108)

有问题的代码:

   @Override
public int returnAttachmentCount(int mnoticesid) {
session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery("select count(*) from GroupAttachments as a where a.mnotedata.mnoticesid=:mnoticesid");
query.setParameter("mnoticesid",mnoticesid);
return Integer.valueOf(String.valueOf(query.uniqueResult()));

}

任何帮助都会很好。多谢。 :-)

编辑

人物映射:

   @OneToMany(mappedBy = "person1",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
@Index(name = "canvas1index")
private Set<Canvas> canvas1 = new HashSet<Canvas>();

public Set<Canvas> getCanvas1() {
return canvas1;
}

public void setCanvas1(Set<Canvas> canvas1) {

this.canvas1 = canvas1;
}

@OneToMany(mappedBy = "userConversation",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
private Set<Conversation> conversations = new HashSet<Conversation>();

public Set<Conversation> getConversations(){
return this.conversations;
}

public void setConversations(Set<Conversation> conversations){
this.conversations=conversations;
}

@OneToMany(mappedBy = "user1Conversation",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
private Set<Conversation> conversation1 = new HashSet<>();

public Set<Conversation> getConversation1(){
return this.conversation1;
}

public void setConversation1(Set<Conversation> conversation1){
this.conversation1=conversation1;
}


@OneToMany(mappedBy = "replyingPerson",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
@Index(name = "replyingpersonindex")
private Set<Replies> repliesSet = new HashSet<>();

public Set<Replies> getRepliesSet(){
return this.repliesSet;
}

public void setRepliesSet(Set<Replies> repliesSet){
this.repliesSet = repliesSet;
}


@OneToMany(mappedBy = "personStatistics",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
private Set<Statistics> statisticsSet = new HashSet<>();

public Set<Statistics> getStatisticsSet(){
return this.statisticsSet;
}

public void setStatisticsSet(Set<Statistics> statisticsSet){
this.statisticsSet = statisticsSet;
}

以上都是Person映射

Canvas 映射:

 @ManyToOne
@JoinColumn(name = "id",nullable = false)
@JsonIgnore
@Index(name = "person1")
private Person person1;

public Person getPerson1() {
return person1;
}

public void setPerson1(Person person1) {
this.person1 = person1;
}

public int getPerson1id(){
return this.person1.getId();
}

@OneToMany(mappedBy = "canvas2",fetch=FetchType.LAZY, cascade = CascadeType.REMOVE)
@JsonIgnore
@Index(name = "section2")
private Set<Section> section2 = new HashSet<Section>();

public Set<Section> getSection2() {
return section2;
}

public void setSection2(Set<Section> section2) {
this.section2 = section2;
}

这些都是映射,并且完全没有像我描述的那样与 GroupAttachments 相关联。

编辑

完整的堆栈跟踪:

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: Found shared references to a collection: com.journaldev.spring.model.Person.canvas1


type Exception report
message Request processing failed; nested exception is org.hibernate.HibernateException: Found shared references to a collection: com.journaldev.spring.model.Person.canvas1
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: Found shared references to a collection: com.journaldev.spring.model.Person.canvas1
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.filters.ExpiresFilter.doFilter(ExpiresFilter.java:1179)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
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.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
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.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
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:344)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
root cause
org.hibernate.HibernateException: Found shared references to a collection: com.journaldev.spring.model.Person.canvas1
org.hibernate.engine.internal.Collections.processReachableCollection(Collections.java:180)
org.hibernate.event.internal.FlushVisitor.processCollection(FlushVisitor.java:59)
org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:121)
org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:82)
org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:76)
org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:172)
org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)
org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:61)
org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1191)
org.hibernate.internal.SessionImpl.list(SessionImpl.java:1257)
org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:966)
com.journaldev.spring.dao.GroupAttachmentsDAOImpl.returnAttachmentCount(GroupAttachmentsDAOImpl.java:108)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy104.returnAttachmentCount(Unknown Source)
com.journaldev.spring.service.GroupAttachmentsServiceImpl.returnAttachmentCount(GroupAttachmentsServiceImpl.java:228)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy94.returnAttachmentCount(Unknown Source)
com.journaldev.spring.dao.GroupNotesDAOImpl.listGroupNotesBySectionId(GroupNotesDAOImpl.java:137)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy109.listGroupNotesBySectionId(Unknown Source)
com.journaldev.spring.service.GroupNotesServiceImpl.listGroupNotesBySectionId(GroupNotesServiceImpl.java:495)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy90.listGroupNotesBySectionId(Unknown Source)
com.journaldev.spring.controller.PersonController.listNotes(PersonController.java:1442)
com.journaldev.spring.controller.PersonController$$FastClassBySpringCGLIB$$f2c66d65.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:640)
com.journaldev.spring.controller.PersonController$$EnhancerBySpringCGLIB$$4fd054fd_2.listNotes(<generated>)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.filters.ExpiresFilter.doFilter(ExpiresFilter.java:1179)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
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.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
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.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
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:344)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)

列表注释方法:

@Override
public List<Notes> listNotesBySectionId(int sectionid) {
session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery("from Notes as n where n.section1.sectionid=:sectionid and n.noteDisabled=false and n.noteInActive=false order by n.noteorder");
query.setParameter("sectionid", sectionid);

List<Notes> notesList = query.list();
if(notesList.isEmpty()){
return notesList;
} else {
double i = 1.0;
for (Notes notes : notesList) {
notes.setNoteorder(i);
notes.setNotetext(notes.getNotetext().replaceAll("\\r?\\n", "<br/>"));
notes.setNotetag(notes.getNotetag().replaceAll("\\r?\\n", "<br/>"));
i = i + 1;
notes.setAttachCount(this.attachmentService.returnAttachmentCount(notes.getNoticesid()));
}
return notesList;
}
}




Also returnAttachmentCount()

@Override
public int returnAttachmentCount(int noticesid) {
session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery("select count(*) from Attachment as a where a.notedata.noticesid=:noticesid");
query.setParameter("noticesid",noticesid);
return new Integer(String.valueOf(query.uniqueResult()));
}

最佳答案

这是一个反复出现的问题,您可以在建议的解决方案中看到类似的问题 here .基本上,您是将同一个集合实例分配给多个实体实例。

为什么在查询中会出现这种情况?好吧,您没有发布整个堆栈跟踪。在那里我们可能会看到异常实际上是在查询执行前自动刷新期间在脏检查时抛出的——它与这个特定的查询(或 getUniqueResult 方法)无关。

重新检查如何在一对多映射中为 Person.canvas1 分配值(以及其他可能获得对“已使用”集合实例的相同引用的实体)。经验法则是(伪代码):

entity.collection = new ArrayList(otherCollectionInstance);

entity.collection = new ArrayList();
entity.collection.addAll(otherCollectionInstance)

或(在更新已持久化(托管)实体实例的情况下)

entity.collection.clear();
entity.collection.addAll(otherCollectionInstance)

这就是为什么使用字段初始化来将对多关联初始化为空集合是一个很好的约定:

@Entity
public class MyEntity {
@OneToMany(...)
private Collection<MyAssociatedEntity> = new ArrayList<>();
}

编辑

发生这种情况似乎还有一些其他原因,其中一些描述了 here .

在这方面,我看到了您的 Canvas 实体的奇怪映射:

@ManyToOne
@JoinColumn(name = "id",nullable = false)
@JsonIgnore
@Index(name = "person1")
private Person person1;

join 列是否应该是“PERSON_ID”之类的其他内容?

关于java - Spring MVC, hibernate : Found shared references to collection when retreiving data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30372161/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com