我是Hibernate的初学者,我想使用CriteriaQuery从两个以上的表中进行选择。
我有3个表:Cellphone,CellphoneIamges和CellphoneRangedPrices和 我想从这三个表中获取数据。 一个例子:下面的查询从表中获取数据:
Select cellphone.cellphoneID, cellphone.cellphoneName, cellphoneImages.cellphoneImageID, cellphoneImages.cellphoneImageInternalLink,
cellphoneImages.cellphoneImageExternalLink, cellphoneRangedPrices.cellphoneRangedPricesID, cellphoneRangedPrices.cellphoneRangedPriceStart, cellphoneRangedPrices.cellphoneRangedPriceEnd
FROM cellphone, cellphoneImages, cellphoneRangedPrices
WHERE cellphone.cellphoneID = cellphoneImages.cellphoneID AND cellphone.cellphoneID = cellphoneRangedPrices.cellphoneID;
但是我想使用CriteriaQuery来获取它。我试过了。
public List getAllCellphoneData(){
EntityManager em = mySQLDAO.getEm();
try {
CriteriaQuery<Object[]> criteriaQuery = em.getCriteriaBuilder().createQuery(Object[].class);
Root<Cellphone> rootPhone = criteriaQuery.from(Cellphone.class);
Root<CellphoneImages> rootImages = criteriaQuery.from(CellphoneImages.class);
criteriaQuery.multiselect(rootImages,rootPhone);
criteriaQuery.where(em.getCriteriaBuilder().equal(rootImages.get("cellphone"), rootPhone.get("cellphoneID")));
Query query = em.createQuery(criteriaQuery);
List<Object[]> resultList = query.getResultList();
return null;
} catch (Exception e) {
logger.log(Level.SEVERE, "Error when trying get cellphone information.", e);
throw new PersistenceException(e);
} finally {
em.close();
}
}
它正在工作,但是我不知道如何在此criteriaQuery中添加更多的类。
我想帮助您了解如何做。 实际上,上面的方法可以从Cellphone和CellphoneImages表中获取数据,但是我不知道我该怎么做才能从另一个表(CellphoneRangedPrices)中获取数据。
我尝试在其他stackoberflow帖子中搜索,但找不到。
注意:表CellphoneRagedPrice具有来自Cellphone表的外键(类似于CellphoneImages表)
非常感谢您的帮助
答案 0 :(得分:0)
我找到了解决问题的方法。 基本上,我需要将来自第三张表的另一个.Class添加到同一“ WHERE”子句中。
criteriaQuery.multiselect(rootPhone,rootImages,rootRangedPrices);
criteriaQuery.where(em.getCriteriaBuilder().equal(rootImages.get("cellphone"), rootPhone.get("cellphoneID")),
em.getCriteriaBuilder().equal(rootPhone.get("cellphoneID"), rootRangedPrices.get("cellPhone")));
请遵循完整的代码:
public List<CellphoneVO> getAllCellphoneData(){
EntityManager em = mySQLDAO.getEm();
List<CellphoneVO> cellphoneVOList = new ArrayList<>();
try {
CriteriaQuery<Object[]> criteriaQuery = em.getCriteriaBuilder().createQuery(Object[].class);
Root<Cellphone> rootPhone = criteriaQuery.from(Cellphone.class);
Root<CellphoneImages> rootImages = criteriaQuery.from(CellphoneImages.class);
Root<CellphoneRangedPrices> rootRangedPrices = criteriaQuery.from(CellphoneRangedPrices.class);
criteriaQuery.multiselect(rootPhone,rootImages,rootRangedPrices);
criteriaQuery.where(em.getCriteriaBuilder().equal(rootImages.get("cellphone"), rootPhone.get("cellphoneID")),
em.getCriteriaBuilder().equal(rootPhone.get("cellphoneID"), rootRangedPrices.get("cellPhone")));
Query query = em.createQuery(criteriaQuery);
List<Object[]> resultList = query.getResultList();
for(Object[] objects : resultList){
CellphoneVO cellphoneVO = new CellphoneVO();
Cellphone cellphone = (Cellphone)objects[0];
CellphoneImages cellphoneImages = (CellphoneImages)objects[1];
CellphoneRangedPrices cellphoneRangedPrices = (CellphoneRangedPrices)objects[2];
cellphoneVO.setCellphone(cellphone);
cellphoneVO.setCellphoneImages(cellphoneImages);
cellphoneVO.setCellphoneRangedPrices(cellphoneRangedPrices);
cellphoneVOList.add(cellphoneVO);
}
return cellphoneVOList;
} catch (Exception e) {
logger.log(Level.SEVERE, "Error when trying get cellphones data.", e);
throw new PersistenceException(e);
} finally {
em.close();
}
}