3

product_template、product_account、product_style 和 product_account 是表。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();

我只得到 product_account 的 id,但我需要 product_template 的 id。任何将不胜感激。提前致谢。

4

2 回答 2

3

这个

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();
于 2013-06-26T13:06:08.047 回答
1
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();

顺便说一句,你的代码已经工作了吗?,因为缺少第一个标准类

于 2013-06-17T16:50:52.890 回答