0

我正在尝试从 BatchEntity 实体中获取 batchInfoEntityList,如下所示:

@Entity(name = "batch")
public class BatchEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long batchId;

    @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH,
        CascadeType.REMOVE }, mappedBy="batchEntity")
    @Fetch(FetchMode.SELECT)
    private List<BatchInfoEntity> batchInfoEntityList;

}

我的 BatchInfoEntity 是:

@Entity(name = "batch_info")
public class BatchInfoEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long batchInfoId;

    @ManyToOne(optional = false, cascade = { CascadeType.REFRESH }, fetch =         FetchType.EAGER, targetEntity = BatchEntity.class)
    @JoinColumn(name = "batchId", referencedColumnName = "batchId")
    private BatchEntity batchEntity; 
}

但是当我得到 batchEntity 实例时,它的 batchInfoEntityList 是空的。我无法理解为什么会这样。

我的 DAO 是

public List<BatchEntity> getA()
        throws Exception {

    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        @SuppressWarnings("unchecked")
        List<BatchEntity> batchEntityList = session
                .createQuery(
                        "select b from "
                                + BatchEntity.class.getName()
                                + " b ")


        return batchEntityList;
    }  finally {
        session.close();
        // session.close();
    }

}
4

0 回答 0