想要帮助使用hibernate标准从1:*关联中的3个表中获取数据

时间:2012-02-14 06:50:39

标签: hibernate

我想从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]]

我有以下问题:

  1. 为什么创建太多查询,因为只有第一个查询就足够了 还好吗?
  2. 为什么要返回所有数据?是否所有查询都在数据中被触发 基地?
  3. 请给我使用代码示例?

1 个答案:

答案 0 :(得分:1)

  1. 第一个查询连接到所需的表但过滤结果集,因此可能不会返回所有需要的子表记录,因此Hibernate不会使用该数据来初始化子集合
  2. 可能禁用延迟加载关联,或者在显示代码后访问关联
  3. 子查询帮助

    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();