3

product_template, product_account, product_style and product_account are tables. mysql command:

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 command is working fine but I don't know how to implement in criteria.
My code:

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

Iam getting only id's of product_account but I need id's of product_template. Any would be appreciated. Thanks in advance.

vivek
  • 4,599
  • 3
  • 25
  • 37

2 Answers2

3

Trt this

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();
vicky
  • 1,046
  • 2
  • 12
  • 25
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();

btw, is your code already working?, because the first criteria class was missing

Ziul
  • 883
  • 1
  • 13
  • 24