【问题标题】:Hibernate error: found shared references休眠错误:找到共享引用
【发布时间】:2018-03-29 10:37:46
【问题描述】:

请求处理失败;嵌套异常是 org.springframework.orm.hibernate5.HibernateSystemException:找到 对集合的共享引用: sabeja.entity.ClassificatorObject.apartmentPayers;嵌套异常是 org.hibernate.HibernateException:发现共享引用 集合:sabeja.entity.ClassificatorObject.apartmentPayers

我已经搜索了 2 天的解决方案。我在 stackoverflow 中找到了解决方案,但它们对我没有帮助。我禁用了部分代码,但我没有发现问题。

分类器对象实体

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<ServicePeriod> periods;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_id", insertable=false, updatable=false)
    private Set<ServiceOption> 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<ApartmentPayer> apartmentPayers;



    public Set<ApartmentPayer> getApartmentPayers() {
        return apartmentPayers;
    }

    public void setApartmentPayers(Set<ApartmentPayer> 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<ServicePeriod> periods, Set<ServiceOption> 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<ServicePeriod> getPeriods() {
//      return periods;
//  }
//
//  public void setPeriods(Set<ServicePeriod> periods) {
//      this.periods = periods;
//  }

    public Set<ServiceOption> getOptions() {
        return options;
    }

    public void setOptions(Set<ServiceOption> 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 + "]";
    }

}

ApartmentPayer 实体

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<ServiceOption> 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<ServiceOption> getOptions() {
        return options;
    }



    public void setOptions(Set<ServiceOption> 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 + "]";
    }

}

DAOIMPL

Query<Apartment> 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);

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    当您有某种“循环”引用时,在某些特定情况下可能会发生此错误,但在这种情况下,您的类中没有使用同一类的字段。

    因此错误必须在对方法setApartmentPayers()的调用中,您需要检查对该方法的所有调用并确保您没有将同一个集合分配给两个不同的实体。

    【讨论】:

      猜你喜欢
      • 2014-02-26
      • 2011-10-05
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      相关资源
      最近更新 更多