Collections Hibernate无法同时为父子孙单向关联提取多个包

Collections Hibernate无法同时为父子孙单向关联提取多个包,collections,Collections,直截了当。我收到了由以下原因引起的Hibernate异常:org.Hibernate.loader.MultipleBagFetchException:无法同时提取多个行李 我在一个父--(1-*)-->子--(1-*)-->孙-->单向关系中有3个实体: NetworkElement->NetworkElementInterface->NetworkElementInterfaceCounters 在这两种关系上,我都定义了FetchType.LAZY @Entity @Table(name

直截了当。我收到了由以下原因引起的Hibernate异常
:org.Hibernate.loader.MultipleBagFetchException:无法同时提取多个行李

我在一个
父--(1-*)-->子--(1-*)-->孙-->单向关系中有3个实体:

NetworkElement->NetworkElementInterface->NetworkElementInterfaceCounters

在这两种关系上,我都定义了
FetchType.LAZY

@Entity
@Table(name = "NETWORK_ELEMENTS")
public class NetworkElement extends Device {

    @Id
    @Column(name = "ID_DEVICE")
    private Long id;

    @ManyToOne(targetEntity = DeviceLocation.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_DEVICE_LOCATION")
    private DeviceLocation deviceLocation;

    @Column(name = "MODEL", length = 30)
    private String model;

    @Column(name = "TYPE", length = 30)
    private String type;

    @Column(name = "IMAGE", length = 50)
    private String image;

    @OneToMany(orphanRemoval = true, fetch = FetchType.LAZY, targetEntity = NetworkElementInterface.class, cascade=CascadeType.ALL)
    @Size(min = 0, max = 99)
    private List<NetworkElementInterface> interfaces;
}

@Entity
@Table(name = "NETWORK_ELEMENT_INTERFACES")
public class NetworkElementInterface {

    @Id
    @Column(name = "ID_NETWORK_ELEMENT_INTERFACES")
    protected Long id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "SPEED")
    private Long speed;

    @Column(name = "DUPLEX")
    private String duplex;

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "NETWORK_ELEMENT_INTERFACE_COUNTERS", joinColumns = @JoinColumn(name = "ID_NETWORK_ELEMENT_INTERFACE"))
    @Type(type = "com.netsuite.wind.entity.NetworkElementInterfaceCounters")
    private List<NetworkElementInterfaceCounters> interfaceCountersHistory;
}


@Embeddable
public class NetworkElementInterfaceCounters {

    private Long inputErrors;

    private Long inputDrops;

    private Long inputDiscards;

    private Long inputCRCErrors;

    private Long inputFifoErrors;

    private Long outputErros;

    private Long outputDrops;

    private Long outputCRCErrors;

    private Long outputFifoErrors;
}
因此:
1)
Query Query=session.createQuery(“从NetworkElement中选择ne加入获取ne.interfaces nei加入获取nei.interfaceCountersHistory,其中ne.id=:id”)
试着让它工作

为了避免这种异常,我想我遗漏了一些注释。我注意到在一些示例中,他们使用的是
@IndexColumn
,但此注释已被弃用。。非常感谢您的指导

@Transactional()
@SuppressWarnings("unchecked")
public NetworkElement getByIdWithInterfaces(Long id) {
    final Session session = sessionFactory.getCurrentSession();

    ------------------------------------------------------        
    // This works well when I only want children to by populated
    Query query = session.createQuery("SELECT ne FROM NetworkElement ne JOIN FETCH ne.interfaces WHERE ne.id = :id");
    ------------------------------------------------------
    // This doesn't work when I also try to fetch grandchildren. I am given Hibernate exception: "cannot simultaneously fetch multiple bags"
    Query query = session.createQuery("SELECT ne FROM NetworkElement ne JOIN FETCH ne.interfaces nei JOIN FETCH nei.interfaceCountersHistory WHERE ne.id = :id");
    ------------------------------------------------------
    // Eventually I tried "FETCH ALL PROPERTIES" which also doesn't work.
    Query query = session.createQuery("FROM NetworkElement ne FETCH ALL PROPERTIES WHERE ne.id = :id");
    ------------------------------------------------------

    query.setParameter("id", id);

    NetworkElement result = null;

    try {
        result = (NetworkElement) query.uniqueResult();
    } catch (NoResultException e) {
        e.printStackTrace();
    }

    return result;
}