无法使用 Hibernate Criteria 获取 OneToMany 关系
Unable to fetch OneToMany relationship using Hibernate Criteria
我正在尝试从数据库中获取 5 个属性。我能够获得 OneToMany 关系的所有 4 个例外。
Dua.class
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "DUA_DATA_FIL_ID")
@LazyCollection(LazyCollectionOption.TRUE)
@Fetch(FetchMode.SELECT)
private List<DuaDataFileYearVO> duaDataFileYearList = new ArrayList<DuaDataFileYearVO>();
Hibernate 标准
public DuaDataFileShipListObj getDuaInfoForShipping(String duaNumber, DisseminatorListObj disseminatorCodeListObj,
int index) {
DuaDataFileShipListObj dataFileShipList = null;
List<DuaDataFileShipVO> dataFileShipVO = new ArrayList<DuaDataFileShipVO>();
List<DuaDataFileVO> dataFileVOs = new ArrayList<DuaDataFileVO>();
Session session = null;
try {
session = getSessionFactory().openSession();
Criteria criteria = session.createCriteria(DuaDataFileVO.class, "dataFile");
criteria.add(Restrictions.eq("duaNum", duaNumber))
// .add(Restrictions.eq("duaDataFileYearList.duaDataFileId", 677L))
.add(Restrictions.in("dataDisseminatorCode", disseminatorCodeListObj.getDisseminatorCodes()));
criteria.createAlias("dataFile.refDataFileExtractingPCTVO", "refDataFileExtractingPCTVO")
.createAlias("dataFile.refDataFileVO", "refDataFileVO")
.createAlias("dataFile.duaDataFileYearList", "duaDataFileYearList").list();
criteria.setProjection(Projections.projectionList()
.add(Projections.property("duaDataFileId"), "duaDataFileId")
.add(Projections.property("dataDisseminatorCode"), "dataDisseminatorCode")
.add(Projections.property("refDataFileExtractingPCTVO"), "refDataFileExtractingPCTVO")
.add(Projections.property("refDataFileVO"), "refDataFileVO"));
// criteria.setFirstResult(0);
criteria.setMaxResults(index);
criteria.setResultTransformer(Transformers.aliasToBean(DuaDataFileVO.class));
dataFileVOs = criteria.list();
乍一看你的问题,你似乎在试图获得 属性: duaDataFileYearList
。在您的 projectionList 中,我没有看到 属性 包含在内,而其他四个包含在内。
更新: 完成此操作的另一种方法是获取所需的整个 DuaDataFileVO
对象,然后获取 DuaDataFileVO.duaDateFileYearList
属性 从那里,而不是试图通过属性获取实体。
我正在尝试从数据库中获取 5 个属性。我能够获得 OneToMany 关系的所有 4 个例外。
Dua.class
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "DUA_DATA_FIL_ID")
@LazyCollection(LazyCollectionOption.TRUE)
@Fetch(FetchMode.SELECT)
private List<DuaDataFileYearVO> duaDataFileYearList = new ArrayList<DuaDataFileYearVO>();
Hibernate 标准
public DuaDataFileShipListObj getDuaInfoForShipping(String duaNumber, DisseminatorListObj disseminatorCodeListObj,
int index) {
DuaDataFileShipListObj dataFileShipList = null;
List<DuaDataFileShipVO> dataFileShipVO = new ArrayList<DuaDataFileShipVO>();
List<DuaDataFileVO> dataFileVOs = new ArrayList<DuaDataFileVO>();
Session session = null;
try {
session = getSessionFactory().openSession();
Criteria criteria = session.createCriteria(DuaDataFileVO.class, "dataFile");
criteria.add(Restrictions.eq("duaNum", duaNumber))
// .add(Restrictions.eq("duaDataFileYearList.duaDataFileId", 677L))
.add(Restrictions.in("dataDisseminatorCode", disseminatorCodeListObj.getDisseminatorCodes()));
criteria.createAlias("dataFile.refDataFileExtractingPCTVO", "refDataFileExtractingPCTVO")
.createAlias("dataFile.refDataFileVO", "refDataFileVO")
.createAlias("dataFile.duaDataFileYearList", "duaDataFileYearList").list();
criteria.setProjection(Projections.projectionList()
.add(Projections.property("duaDataFileId"), "duaDataFileId")
.add(Projections.property("dataDisseminatorCode"), "dataDisseminatorCode")
.add(Projections.property("refDataFileExtractingPCTVO"), "refDataFileExtractingPCTVO")
.add(Projections.property("refDataFileVO"), "refDataFileVO"));
// criteria.setFirstResult(0);
criteria.setMaxResults(index);
criteria.setResultTransformer(Transformers.aliasToBean(DuaDataFileVO.class));
dataFileVOs = criteria.list();
乍一看你的问题,你似乎在试图获得 属性: duaDataFileYearList
。在您的 projectionList 中,我没有看到 属性 包含在内,而其他四个包含在内。
更新: 完成此操作的另一种方法是获取所需的整个 DuaDataFileVO
对象,然后获取 DuaDataFileVO.duaDateFileYearList
属性 从那里,而不是试图通过属性获取实体。