title: Spring-MVC, Hibernate : Found shared references to collection when retreiving data
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>Error code :  </p>
  
  <pre><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></pre>
  
  <p>Code in question :</p>
  
  <pre><code>   @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()));
  
      }
  </code></pre>
  
  <p>Any help would be nice. Thanks a lot. :-)</p>
  
  <p><em>Edit</em></p>
  
  <p>Person mapping :</p>
  
  <pre><code>   @OneToMany(mappedBy = "person1",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
      @Index(name = "canvas1index")
      private Set&lt;Canvas&gt; canvas1 = new HashSet&lt;Canvas&gt;();
  
      public Set&lt;Canvas&gt; getCanvas1() {
          return canvas1;
      }
  
      public void setCanvas1(Set&lt;Canvas&gt; canvas1) {
  
          this.canvas1 = canvas1;
      }
  
      @OneToMany(mappedBy = "userConversation",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
      private Set&lt;Conversation&gt; conversations = new HashSet&lt;Conversation&gt;();
  
      public Set&lt;Conversation&gt; getConversations(){
          return this.conversations;
      }
  
      public void setConversations(Set&lt;Conversation&gt; conversations){
          this.conversations=conversations;
      }
  
      @OneToMany(mappedBy = "user1Conversation",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
      private Set&lt;Conversation&gt; conversation1 = new HashSet&lt;&gt;();
  
      public Set&lt;Conversation&gt; getConversation1(){
          return this.conversation1;
      }
  
      public void setConversation1(Set&lt;Conversation&gt; conversation1){
          this.conversation1=conversation1;
      }
  
  
      @OneToMany(mappedBy = "replyingPerson",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
      @Index(name = "replyingpersonindex")
      private Set&lt;Replies&gt; repliesSet = new HashSet&lt;&gt;();
  
      public Set&lt;Replies&gt; getRepliesSet(){
          return this.repliesSet;
      }
  
      public void setRepliesSet(Set&lt;Replies&gt; repliesSet){
          this.repliesSet = repliesSet;
      }
  
  
      @OneToMany(mappedBy = "personStatistics",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
      private Set&lt;Statistics&gt; statisticsSet = new HashSet&lt;&gt;();
  
      public Set&lt;Statistics&gt; getStatisticsSet(){
          return this.statisticsSet;
      }
  
      public void setStatisticsSet(Set&lt;Statistics&gt; statisticsSet){
          this.statisticsSet = statisticsSet;
      }
  </code></pre>
  
  <p>Above are all Person mappings</p>
  
  <p>Canvas mappings :</p>
  
  <pre><code> @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&lt;Section&gt; section2 = new HashSet&lt;Section&gt;();
  
      public Set&lt;Section&gt; getSection2() {
          return section2;
      }
  
      public void setSection2(Set&lt;Section&gt; section2) {
          this.section2 = section2;
      }
  </code></pre>
  
  <p>These are all the mappings, and completely unrealted to GroupAttachments as I described. </p>
  
  <p><em>Edit</em></p>
  
  <p>Complete stack trace :</p>
  
  <pre><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
  
  
  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(&lt;generated&gt;)
          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(&lt;generated&gt;)
          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)
  </code></pre>
  
  <p>ListNotesMethod :</p>
  
  <pre><code>@Override
      public List&lt;Notes&gt; 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&lt;Notes&gt; 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", "&lt;br/&gt;"));
                  notes.setNotetag(notes.getNotetag().replaceAll("\\r?\\n", "&lt;br/&gt;"));
                  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()));
      }
  </code></pre>