我想从1:*关联中的3个表中获取数据,其中连接表位于下面所示 -
之间表ProductCategory ==> J Table ProductCategory_Product ==>表产品==> J Table Product_ProdSpec ==>表ProdSpec
现在我想根据产品规格检索记录并将其显示在HTML表格中。
我已经使用了hibernate Criteria查询,如下所示 -
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria offerCriteria = session.createCriteria(ProductCategory.class)
.createAlias("productList","productList")
.createAlias("productList.prodSpecList","prodSpecList")
.add(Restrictions.like("prodSpecList.prodSpecDesc", "%dual Sim%"))
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List list = offerCriteria.list();
Hibernate正在创建以下查询 -
Hibernate: select this_.productCategoryNumber as productC1_0_2_, this_.ProductCategoryType as ProductC2_0_2_, productlis4_.productCategoryNumber as productC1_0_, productlis1_.productNumber as productN2_, productlis1_.productNumber as productN1_1_0_, productlis1_.productCompanyName as productC2_1_0_, prodspecli6_.productNumber as productN1_1_, prodspecli2_.prodSpecNumber as prodSpec2_, prodspecli2_.prodSpecNumber as prodSpec1_2_1_, prodspecli2_.prodSpecDesc as prodSpec2_2_1_ from ProductCategory this_ inner join ProductCategory_Product productlis4_ on this_.productCategoryNumber=productlis4_.productCategoryNumber inner join Product productlis1_ on productlis4_.productNumber=productlis1_.productNumber inner join Product_ProdSpec prodspecli6_ on productlis1_.productNumber=prodspecli6_.productNumber inner join ProdSpec prodspecli2_ on prodspecli6_.prodSpecNumber=prodspecli2_.prodSpecNumber where prodspecli2_.prodSpecDesc like ?
Hibernate: select productlis0_.productCategoryNumber as productC1_0_1_, productlis0_.productNumber as productN2_1_, product1_.productNumber as productN1_1_0_, product1_.productCompanyName as productC2_1_0_ from ProductCategory_Product productlis0_ inner join Product product1_ on productlis0_.productNumber=product1_.productNumber where productlis0_.productCategoryNumber=?
Hibernate: select prodspecli0_.productNumber as productN1_1_1_, prodspecli0_.prodSpecNumber as prodSpec2_1_, prodspec1_.prodSpecNumber as prodSpec1_2_0_, prodspec1_.prodSpecDesc as prodSpec2_2_0_ from Product_ProdSpec prodspecli0_ inner join ProdSpec prodspec1_ on prodspecli0_.prodSpecNumber=prodspec1_.prodSpecNumber where prodspecli0_.productNumber=?
Hibernate: select prodspecli0_.productNumber as productN1_1_1_, prodspecli0_.prodSpecNumber as prodSpec2_1_, prodspec1_.prodSpecNumber as prodSpec1_2_0_, prodspec1_.prodSpecDesc as prodSpec2_2_0_ from Product_ProdSpec prodspecli0_ inner join ProdSpec prodspec1_ on prodspecli0_.prodSpecNumber=prodspec1_.prodSpecNumber where prodspecli0_.productNumber=?
Hibernate: select prodspecli0_.productNumber as productN1_1_1_, prodspecli0_.prodSpecNumber as prodSpec2_1_, prodspec1_.prodSpecNumber as prodSpec1_2_0_, prodspec1_.prodSpecDesc as prodSpec2_2_0_ from Product_ProdSpec prodspecli0_ inner join ProdSpec prodspec1_ on prodspecli0_.prodSpecNumber=prodspec1_.prodSpecNumber where prodspecli0_.productNumber=?
它正在返回以下结果。
productCategoryType :- Mobile[productCompanyName :- Samsung prodSpecList :- [GPS navigation, music (MP3), video (MP4) playback, dual Sim, video calling, built-in cameras, games, office, Bluetooth, WiFi, instant messaging, Internet, e-mail, browsing], productCompanyName :- LG prodSpecList :- [GPS navigation, video (MP4) playback, video calling, games, Bluetooth, instant messaging, e-mail], productCompanyName :- Sony prodSpecList :- [music (MP3), dual Sim, built-in cameras, office, WiFi, Internet, browsing]]
我有以下问题:
答案 0 :(得分:1)
子查询帮助
Session session = HibernateUtil.getSessionFactory().openSession();
// fetch the ids of desired ProductCategories
DetachedCriteria subquery = DetachedCriteria.for(ProductCategory.class)
.createAlias("productList","productList")
.createAlias("productList.prodSpecList","prodSpecList")
.add(Restrictions.like("prodSpecList.prodSpecDesc", "%dual Sim%"))
.setProjection(Projection.Id());
// fetch
Criteria offerCriteria = session.createCriteria(ProductCategory.class)
.add(Subquery.In(Projection.Id(), subquery))
.setFetchMode("productList", FetchMode.Eager)
.setFetchMode("productList.prodSpecList", FetchMode.Eager);
// maybe needed
//.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List list = offerCriteria.list();