java—如何在hibernate中实现多个内部连接

fwzugrvs  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(329)

产品模板、产品帐户、产品样式和产品帐户是表。mysql命令:

  1. select distinct product_template.id as ptid from product_account inner join product on product_account.productId=product.id inner join product_style on product.productStyleId=product_style.id inner join product_template on product_style.productTemplateId=product_template.id where product_account.sellerId=1 and product_account.productAccountType=1;

mysql命令运行良好,但我不知道如何在标准中实现。
我的代码:

  1. Criteria c = createCriteria(ProductAccount.class);
  2. ProjectionList projectionList = Projections.projectionList();
  3. c.add(Restrictions.eq("seller", query.getSeller()));
  4. c.add(Restrictions.eq("productAccountType", query.getProductAccountType()));
  5. c.createCriteria("product").createCriteria("productStyle").createCriteria("productTemplate");
  6. c.setProjection(Projections.distinct(Projections.property("id")));
  7. List<Object> objects = c.list();

我只得到产品\帐户的id,但我需要产品\模板的id。如有任何疑问,我们将不胜感激。提前谢谢。

lo8azlld

lo8azlld1#

trt这个

  1. Criteria c = createCriteria();
  2. c.add(Restrictions.eq("seller", query.getSeller()));
  3. c.add(Restrictions.eq("productAccountType", query.getProductAccountType()));
  4. c.createCriteria("product").createCriteria("productStyle").createCriteria("productTemplate", "pt");
  5. c.setProjection(Projections.distinct(Projections.property("pt.id")));
  6. List<Object> objects = c.list();
qzlgjiam

qzlgjiam2#

  1. Criteria c = createCriteria(ProductAccount.class);
  2. c.add(Restrictions.eq("seller", query.getSeller()));
  3. c.add(Restrictions.eq("productAccountType", query.getProductAccountType()));
  4. c.createCriteria("product")
  5. .createCriteria("productStyle")
  6. .createCriteria("productTemplate", "pt"); //This needs an alias
  7. c.setProjection(Projections.projectionList()
  8. .add( Projections.distinct( Projections.property("id") ) )
  9. .add( Projections.property("pt.id") )); //Add it to the projection list
  10. List<Object> objects = c.list();

顺便说一句,你的代码已经运行了吗?因为缺少第一个criteria类

相关问题