Join with criteria

Asked

Viewed 84 times

-1

I have the following entities:

public class Mapa {
   //many to one
   private ItemMapa itemMapa;
}

public class ItemMapa {
   //many to one
   private Classe classe;
}

public class Classe {
   private Long id;
}

I need to implement a query using criteria, but I don’t know how to make a Join to get to the object Classe from the object Mapa. In hql would do something like this:

String hql = "FROM Mapa m WHERE m.itemMapa.classe.idClasse = :idClasse";

1 answer

0


Try this:

DetachedCriteria criteria = DetachedCriteria.forClass(Mapa.class);
criteria.createAlias("itemMapa", "ITEM", JoinType.INNER_JOIN);
criteria.createAlias("ITEM.classe", "CLASSE" , JoinType.INNER_JOIN);
criteria.add(Restrictions.eq("CLASSE.id", id));
Mapa mapa = (Mapa) executeCriteriaUniqueResult(criteria);

Browser other questions tagged

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