title: Hibernate error: found shared references
body: <blockquote>
    <p>Request processing failed; nested exception is
    org.springframework.orm.hibernate5.HibernateSystemException: Found
    shared references to a collection:
    sabeja.entity.ClassificatorObject.apartmentPayers; nested exception is
    org.hibernate.HibernateException: Found shared references to a
    collection: sabeja.entity.ClassificatorObject.apartmentPayers</p>
  </blockquote>
  
  <p>I have been searching solution for 2 days. I found solutions in the stackoverflow, but they didn't help for me. I disabled parts of code, but I haven't caught problem.</p>
  
  <p>ClassificatorObject Entity</p>
  
  <pre><code>package xm.entity;
  
  import java.io.Serializable;
  import java.util.Set;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.OneToMany;
  import javax.persistence.OneToOne;
  import javax.persistence.OrderBy;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="managment_responsibilities_object")
  public class ClassificatorObject implements Serializable{
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="id")
      private int id;
  
      @Column(name="responsibility_id")
      private int responsibilityId;
  
      @Column(name="object_id")
      private int objectId; 
  
      @Column(name="is_enabled")
      private int isEnabled;
  
      @Column(name="by_area")
      private int byArea; 
  
      @Column(name="by_people")
      private int byPeople; 
  
      @Column(name="by_flat")
      private int byFlat; 
  
      @Column(name="is_general")
      private int isGeneral;
  
      @Column(name="sort")
      private int sort;
  
      @Column(name="not_counting")
      private int notCounting;
  
  //  @OneToMany(fetch = FetchType.LAZY, mappedBy = "classificatorObject", cascade = CascadeType.ALL)
  //  @OrderBy("month")
  //  private Set&lt;ServicePeriod&gt; periods;
  
      @OneToMany(fetch = FetchType.LAZY)
      @JoinColumn(name = "service_id", insertable=false, updatable=false)
      private Set&lt;ServiceOption&gt; options;
  
      @OneToMany(fetch = FetchType.LAZY)
      //@JoinColumn(name = "service_id", insertable=false, updatable=false)
      @JoinColumn(
              name = "object_id", 
              referencedColumnName = "object_id",
              insertable=false, updatable=false
          )
  
      private Set&lt;ApartmentPayer&gt; apartmentPayers;
  
  
  
      public Set&lt;ApartmentPayer&gt; getApartmentPayers() {
          return apartmentPayers;
      }
  
      public void setApartmentPayers(Set&lt;ApartmentPayer&gt; apartmentPayers) {
          this.apartmentPayers = apartmentPayers;
      }
  
  
  
      @Column(name="parent_service_id")
      private int parentServiceId;
  
      @Column(name="has_children")
      private int hasChildren;
  
      @OneToOne(fetch = FetchType.LAZY)
      @JoinColumn(name="responsibility_id", insertable=false, updatable=false)
      private Classificator classificator;
  
      @OneToOne(fetch = FetchType.LAZY)
      @JoinColumn(name="parent_service_id", insertable=false, updatable=false)
      private ClassificatorObject parentService;
  
  
  
  //CONSTRUCTORS: 
  
      public ClassificatorObject() {
      }
  
      public ClassificatorObject(int responsibilityId, int objectId, int isEnabled, int byArea, int byPeople, int byFlat,
              int isGeneral, int sort, int notCounting, Set&lt;ServicePeriod&gt; periods, Set&lt;ServiceOption&gt; options,
              int parentServiceId, int hasChildren, Classificator classificator) {
          super();
          this.responsibilityId = responsibilityId;
          this.objectId = objectId;
          this.isEnabled = isEnabled;
          this.byArea = byArea;
          this.byPeople = byPeople;
          this.byFlat = byFlat;
          this.isGeneral = isGeneral;
          this.sort = sort;
          this.notCounting = notCounting;
      //  this.periods = periods;
          this.options = options;
          this.parentServiceId = parentServiceId;
          this.hasChildren = hasChildren;
          this.classificator = classificator;
      }
  
  //GETTERS SETTERS:
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public int getResponsibilityId() {
          return responsibilityId;
      }
  
      public void setResponsibilityId(int responsibilityId) {
          this.responsibilityId = responsibilityId;
      }
  
      public int getObjectId() {
          return objectId;
      }
  
      public void setObjectId(int objectId) {
          this.objectId = objectId;
      }
  
      public int getIsEnabled() {
          return isEnabled;
      }
  
      public void setIsEnabled(int isEnabled) {
          this.isEnabled = isEnabled;
      }
  
      public int getByArea() {
          return byArea;
      }
  
      public void setByArea(int byArea) {
          this.byArea = byArea;
      }
  
      public int getByPeople() {
          return byPeople;
      }
  
      public void setByPeople(int byPeople) {
          this.byPeople = byPeople;
      }
  
      public int getByFlat() {
          return byFlat;
      }
  
      public void setByFlat(int byFlat) {
          this.byFlat = byFlat;
      }
  
      public int getIsGeneral() {
          return isGeneral;
      }
  
      public void setIsGeneral(int isGeneral) {
          this.isGeneral = isGeneral;
      }
  
      public int getSort() {
          return sort;
      }
  
      public void setSort(int sort) {
          this.sort = sort;
      }
  
      public int getNotCounting() {
          return notCounting;
      }
  
  
      public void setNotCounting(int notCounting) {
          this.notCounting = notCounting;
      }
  
  //  public Set&lt;ServicePeriod&gt; getPeriods() {
  //      return periods;
  //  }
  //
  //  public void setPeriods(Set&lt;ServicePeriod&gt; periods) {
  //      this.periods = periods;
  //  }
  
      public Set&lt;ServiceOption&gt; getOptions() {
          return options;
      }
  
      public void setOptions(Set&lt;ServiceOption&gt; options) {
          this.options = options;
      }
  
      public int getParentServiceId() {
          return parentServiceId;
      }
  
      public void setParentServiceId(int parentServiceId) {
          this.parentServiceId = parentServiceId;
      }   
  
      public int getHasChildren() {
          return hasChildren;
      }
  
      public void setHasChildren(int hasChildren) {
          this.hasChildren = hasChildren;
      }
  
      public Classificator getClassificator() {
          return classificator;
      }
  
      public void setClassificator(Classificator classificator) {
          this.classificator = classificator;
      }   
  
  
  
      public ClassificatorObject getParentService() {
          return parentService;
      }
  
      public void setParentService(ClassificatorObject parentService) {
          this.parentService = parentService;
      }
  
      @Override
      public String toString() {
          return "ClassificatorObject [id=" + id + ", responsibilityId=" + responsibilityId + ", objectId=" + objectId
                  + ", isEnabled=" + isEnabled + ", byArea=" + byArea + ", byPeople=" + byPeople + ", byFlat=" + byFlat
                  + ", isGeneral=" + isGeneral + ", sort=" + sort + ", notCounting=" + notCounting + ", parentServiceId="
                  + parentServiceId + ", hasChildren=" + hasChildren + "]";
      }
  
  }
  </code></pre>
  
  <p>ApartmentPayer Entity</p>
  
  <pre><code>package sabeja.entity;
  
  import java.math.BigDecimal;
  import java.util.Set;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.OneToMany;
  import javax.persistence.OneToOne;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="sb_flat_payers")
  public class ApartmentPayer {
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="id")
      private Integer id;
  
      @Column(name="flat_id")
      private int flatId;
  
  
      @Column(name="payer_id")
      private int payerId;
  
      @Column(name="object_id")
      private int objectId;
  
      @Column(name="area", columnDefinition="Decimal(12,2)")
      private BigDecimal area;
  
      @Column(name="people_number")
      private int peopleNumber;
  
      @OneToMany(fetch = FetchType.LAZY)
      @JoinColumn(name="flatPayer_id", insertable=false, updatable=false)
      private Set&lt;ServiceOption&gt; options;
  
      @OneToOne(fetch = FetchType.LAZY)
      @JoinColumn(name="payer_id", insertable= false, updatable= false)
      private User user;
  
  //  @ManyToOne(fetch = FetchType.LAZY)
  //  @JoinColumn(name="object_id", insertable=false, updatable=false)
  //  private TheObject theObject;
  
      public ApartmentPayer() {
          super();
      }
  
  
  
      public User getUser() {
          return user;
      }
  
  
  
      public void setUser(User user) {
          this.user = user;
      }
  
  
  
      public Set&lt;ServiceOption&gt; getOptions() {
          return options;
      }
  
  
  
      public void setOptions(Set&lt;ServiceOption&gt; options) {
          this.options = options;
      }
  
  
  
      public Integer getId() {
          return id;
      }
  
  
      public void setId(Integer id) {
          this.id = id;
      }
  
  
      public int getFlatId() {
          return flatId;
      }
  
  
      public void setFlatId(int flatId) {
          this.flatId = flatId;
      }
  
  
      public int getPayerId() {
          return payerId;
      }
  
  
      public void setPayerId(int payerId) {
          this.payerId = payerId;
      }
  
  
  
  
      public BigDecimal getArea() {
          return area;
      }
  
  
  
      public void setArea(BigDecimal area) {
          this.area = area;
      }
  
  
  
      public int getPeopleNumber() {
          return peopleNumber;
      }
  
  
  
      public void setPeopleNumber(int peopleNumber) {
          this.peopleNumber = peopleNumber;
      }
  
  
      public int getObjectId() {
          return objectId;
      }
  
  
  
      public void setObjectId(int objectId) {
          this.objectId = objectId;
      }
  
  
  //  public TheObject getTheObject() {
  //      return theObject;
  //  }
  //
  //
  //
  //  public void setTheObject(TheObject theObject) {
  //      this.theObject = theObject;
  //  }
  
  
  
      @Override
      public String toString() {
          return "ApartmentPayer [id=" + id + ", flatId=" + flatId + ", payerId=" + payerId + ", area=" + area
                  + ", peopleNumber=" + peopleNumber + "]";
      }
  
  }
  </code></pre>
  
  <p>DAOIMPL</p>
  
  <pre><code>Query&lt;Apartment&gt; queryResult2 = currentSession
                  .createQuery("SELECT DISTINCT a FROM Apartment a "
                          + "JOIN FETCH a.theObject o "
                          + "JOIN FETCH o.services s "
                          + "JOIN FETCH s.apartmentPayers ap "
                      //  + "LEFT JOIN FETCH s.options op "
                      //  + "JOIN ServicePeriod p ON s.id = p.responsibilities_object_id "
                      //  + "JOIN FETCH ap.user u "
                          + "WHERE a.houseId = :object_id "
                      //  + "AND (op.flatPayerId = ap.id OR op.id is NULL) "
                          //+ "AND (SELECT count(*) FROM DisabledPayer di WHERE  di.serviceId = s.id AND di.flatPayerId = ap.id) = 0"
              //  + "AND (CASE WHEN op.is_disabled = 0 THEN 1 WHEN op.is_disabled = 1 "
              //  + "THEN 0  END) = 1) OR op.id IS NULL"
                  , Apartment.class);
  </code></pre>