Hibernate criteriaQuery createAlias equivalent

5.4k Views Asked by At

I am looking at upgrading my hibernate to use JPA criteriaQuery :-( and need to find a way to implement an alias in JPA eg:

@Entity
public class MyClass {
....
@ManyToOne(..)
public MyDetail getMyDetail() { 
return myDetail;  
}
}

Criteria criteria = session.createCriteria(MyClass.class);
....
criteria.createAlias("myDetail", "md");
criteria.add(Restrictions.eq("md.active", true));

I have been through the hibernate docs but they seem to have only covered trivial examples and glossed over hibernate specific functionality.

Cheers Greg

1

There are 1 best solutions below

1
michalavis On

You can create alias in JPA CriteriaQuery on entityRoot

Root<A> entityRoot = criteriaQuery.from(A);
entityRoot.join("b", JoinType.LEFT).alias("b");