How to make a Criteria Join for this query

Asked

Viewed 1,530 times

0

While researching a little I realized that there are some ways to make a Join using criteria.

One of them is using an alias and the other using a root.

How to do the query below in Criteria using the 2 forms and which of the two would be the best (better in relation to performance)?

Select c.id from entidade c inner join entidade2 e where c.id=e.idC; 

Does anyone know any site with a nice documentation regarding Criterias and Joins?

I know a way to do but returns the whole entity and that’s not quite how I want it because I just want the entity id.

   Criteria c = session.createCriteria(Entidade.class, "C");
     c.createAlias("C.entidades1", "e");
     c.add(Restrictions.eq("c.id", "e.idC"));
     c.list();

1 answer

1


If the criteria you presented is returning the entity you want, just do the setProjection:

Criteria c = session.createCriteria(Entidade.class, "C");
     c.createAlias("C.entidades1", "e");
     c.add(Restrictions.eq("c.id", "e.idC"));
     c.setProjection(Projections.property("c.id"));
     c.list();

With respect to documentation, this site is very good: https://docs.jboss.org/hibernate/orm/3.3/reference/pt-BR/html/querycriteria.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.