postgresql Spring-MVC,Hibernate:检索数据时找到对集合的共享引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30372161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:55:12  来源:igfitidea点击:

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

javaspringhibernatepostgresql

提问by We are Borg

I am working on a Spring-MVC application in which I am using hibernate as the ORM tool as PostgreSQL as the database. Currently I am having a problem when I am accessing data from the database for a few methods. All the times, the error is same. I am not persisting anything, but rather just requesting data.

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

The problem is always happening at getUniqueResult for a 3-4 of the DAO methods. Any help would be nice. Also, I checked the answers, but they give only some scenarios, none of them applicable as I am just querying for data.

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

Error code :

错误代码 :

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)

Code in question :

有问题的代码:

   @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()));

    }

Any help would be nice. Thanks a lot. :-)

你能帮忙的话,我会很高兴。非常感谢。:-)

Edit

编辑

Person mapping :

人物映射:

   @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;
    }

Above are all Person mappings

以上都是Person映射

Canvas mappings :

画布映射:

 @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;
    }

These are all the mappings, and completely unrealted to GroupAttachments as I described.

这些都是所有映射,完全没有实现我所描述的 GroupAttachments。

Edit

编辑

Complete stack trace :

完整的堆栈跟踪:

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.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.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.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.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$fd054fd_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)

ListNotesMethod :

ListNotesMethod :

@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()));
    }

回答by Dragan Bozanovic

This is a recurring question, you can see a similar one with the suggested solutions here. Basically, you are assigning the same collection instance to more than one entity instance.

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

Why does this happen in the query? Well, you did not post the entire stack trace. There we would probably see that the exception is actually thrown on dirty check during automatic flush before query execution - it has nothing to do with this specific query (or the getUniqueResult method).

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

Recheck how you assign values in one-to-many mappings for Person.canvas1 (and for other entities which could get the same reference to the 'already used' collection instance). The rule of thumb is (pseudo-code):

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

entity.collection = new ArrayList(otherCollectionInstance);

or

或者

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

or (in case of an update of an already persisted (managed) entity instance)

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

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

That's why it's a good convention to use field initialization to initialize to-many associations to an empty collection:

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

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

EDIT

编辑

There seems to be some other reasons for this to happen, some of them described here.

发生这种情况似乎还有其他一些原因,其中一些在此处进行了描述。

In that regard I see strange mapping for your Canvas entity:

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

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

Should join column be something else like "PERSON_ID"?

加入列应该是“PERSON_ID”之类的东西吗?

回答by Paul John

This error usually happens where an entity (set of class a) of one type is assigned to a set of another entity (set of class B). In your example - is there a collection of canvas on groupAttachments?

当将一种类型的实体(类 a 的集合)分配给另一个实体的集合(类 B 的集合)时,通常会发生此错误。在您的示例中 - groupAttachments 上是否有画布集合?

Here are couple of examples i found - https://forum.hibernate.org/viewtopic.php?f=1&t=949652

这是我发现的几个例子 - https://forum.hibernate.org/viewtopic.php?f=1&t=949652

Caused by: org.hibernate.HibernateException: Found shared references to a collection

引起:org.hibernate.HibernateException:找到对集合的共享引用