Hibernate @OneToMany失败,"集合与任何会话都没有关联#34;

时间:2015-11-23 08:04:37

标签: java hibernate

这就是重点:我想加载" DeliveryOrderEntity" (代码如下)来自DB(MySQL)。它有一个带有"一对多的外键"关系。它连接" BillsEntity"使用billNumber。为了说清楚 - 我的表ORDER包含列BILL_NUMBER,表BILL包含列BILL_ID(主键)和BILL_NUMBER(列,连接{ {1}}与BILL)。 这是我的代码:

DeliveryOrderEntity.java:

ORDER

BillsEntity.java:

@Entity
@Table(name = "DELIVERY_ORDER", schema = "", catalog = "kursach")
@NamedQueries(value = {
    @NamedQuery(name = "findAllOrders", query = "from DeliveryOrderEntity"),
    @NamedQuery(name = "getOrder", query = "from DeliveryOrderEntity where orderId=:orderId"),
    @NamedQuery(name = "removeOrder", query = "delete from DeliveryOrderEntity where orderId=:orderId")
})
public class DeliveryOrderEntity implements Serializable{
private int orderId;
private Timestamp orderDate;
private String orderName;
private String orderTelephone;
private Integer billNumber;
private Integer regionId;
private List<BillEntity> listBill;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ORDER_ID")
public int getOrderId() {
    return orderId;
}

public void setOrderId(int orderId) {
    this.orderId = orderId;
}

@Basic
@Column(name = "ORDER_DATE")
public Timestamp getOrderDate() {
    return orderDate;
}

public void setOrderDate(Timestamp orderDate) {
    this.orderDate = orderDate;
}

@Basic
@Column(name = "ORDER_NAME")
public String getOrderName() {
    return orderName;
}

public void setOrderName(String orderName) {
    this.orderName = orderName;
}

@Basic
@Column(name = "ORDER_TELEPHONE")
public String getOrderTelephone() {
    return orderTelephone;
}

public void setOrderTelephone(String orderTelephone) {
    this.orderTelephone = orderTelephone;
}

@Basic
@Column(name = "BILL_NUMBER")
public Integer getBillNumber() {
    return billNumber;
}

public void setBillNumber(Integer billNumber) {
    this.billNumber = billNumber;
}

@Basic
@Column(name = "REGION_ID")
public Integer getRegionId() {
    return regionId;
}

public void setRegionId(Integer regionNumber) {
    this.regionId = regionNumber;
}

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "BILL_NUMBER", referencedColumnName = "BILL_NUMBER", nullable = false, insertable = false, updatable = false)
public List<BillEntity> getListBill() {
    return listBill;
}

public void setListBill(List<BillEntity> listBill) {
    this.listBill = listBill;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    DeliveryOrderEntity entity = (DeliveryOrderEntity) o;

    if (orderId != entity.orderId) return false;
    if (orderDate != null ? !orderDate.equals(entity.orderDate) : entity.orderDate != null) return false;
    if (orderName != null ? !orderName.equals(entity.orderName) : entity.orderName != null) return false;
    if (orderTelephone != null ? !orderTelephone.equals(entity.orderTelephone) : entity.orderTelephone != null)
        return false;
    if (billNumber != null ? !billNumber.equals(entity.billNumber) : entity.billNumber != null) return false;
    return !(regionId != null ? !regionId.equals(entity.regionId) : entity.regionId != null);

}

@Override
public int hashCode() {
    int result = orderId;
    result = 31 * result + (orderDate != null ? orderDate.hashCode() : 0);
    result = 31 * result + (orderName != null ? orderName.hashCode() : 0);
    result = 31 * result + (orderTelephone != null ? orderTelephone.hashCode() : 0);
    result = 31 * result + (billNumber != null ? billNumber.hashCode() : 0);
    return result;
}
}

如您所见,我有单向的OneToMany关系。问题是,当我在DELIVERY_ORDER表中有2行具有相同的BILL_NUMBER并且我尝试获取订单列表时 - 它失败了:

@Entity
@Table(name = "BILL")
@NamedQueries({
        @NamedQuery(name = "findAllBills", query = "from BillEntity"),
        @NamedQuery(name = "findAllBillIds", query = "select billNumber from BillEntity group by billNumber"),
        @NamedQuery(name = "findAllBillsById", query = "from BillEntity where billNumber=:billNumber")
})
public class BillEntity implements Serializable {
    private int billId;
    private Integer billNumber;
    private Integer goodCount;
    private Integer goodId;
    private Integer billStatus;
    private GoodsEntity good;

    @Id
    @Column(name = "BILL_ID")
    public int getBillId() {
        return billId;
    }

    public void setBillId(int billId) {
        this.billId = billId;
    }

    @Basic
    @Column(name = "BILL_NUMBER", updatable = false, insertable = false)
    public Integer getBillNumber() {
        return billNumber;
    }

    public void setBillNumber(Integer billNumber) {
        this.billNumber = billNumber;
    }

    @Basic
    @Column(name = "GOOD_COUNT")
    public Integer getGoodCount() {
        return goodCount;
    }

    public void setGoodCount(Integer goodCount) {
        this.goodCount = goodCount;
    }

    @Basic
    @Column(name = "BILL_STATUS")
    public Integer getBillStatus() {
        return billStatus;
    }

    @Basic
    @Column(name = "GOOD_ID")
    public Integer getGoodId() {
        return goodId;
    }

    public void setGoodId(Integer goodId) {
        this.goodId = goodId;
    }

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "GOOD_ID", referencedColumnName = "GOOD_ID", nullable = false, insertable = false, updatable = false)
    public GoodsEntity getGood() {
        return good;
    }

    public void setGood(GoodsEntity good) {
        this.good = good;
    }

    public void setBillStatus(Integer billStatus) {
        this.billStatus = billStatus;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        BillEntity that = (BillEntity) o;

        if (billId != that.billId) return false;
        if (billNumber != null ? !billNumber.equals(that.billNumber) : that.billNumber != null) return false;
        if (goodCount != null ? !goodCount.equals(that.goodCount) : that.goodCount != null) return false;
        if (goodId != null ? !goodId.equals(that.goodId) : that.goodId != null) return false;
        return !(billStatus != null ? !billStatus.equals(that.billStatus) : that.billStatus != null);

    }

    @Override
    public int hashCode() {
        int result = billId;
        result = 31 * result + (billNumber != null ? billNumber.hashCode() : 0);
        result = 31 * result + (goodCount != null ? goodCount.hashCode() : 0);
        result = 31 * result + (goodId != null ? goodId.hashCode() : 0);
        result = 31 * result + (billStatus != null ? billStatus.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "BillEntity{" +
                "billId=" + billId +
                ", billNumber=" + billNumber +
                ", goodCount=" + goodCount +
                ", billStatus=" + billStatus +
                ", good=" + good +
                '}';
    }


}

以下是我获取列表的代码:

Caused by: org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:676)
at org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:1030)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:346)
at org.hibernate.loader.Loader.doList(Loader.java:2522)
at org.hibernate.loader.Loader.doList(Loader.java:2508)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338)
at org.hibernate.loader.Loader.list(Loader.java:2333)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:264)

提前谢谢。

0 个答案:

没有答案