无法在一对多映射中获取数据

时间:2013-09-25 19:35:03

标签: hibernate one-to-many

我试图从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();
    }

}

0 个答案:

没有答案