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

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

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

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命令运行良好,但我不知道如何在标准中实现。
我的代码:

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

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

lo8azlld

lo8azlld1#

trt这个

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

qzlgjiam2#

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

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

相关问题