1

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

gregh
  • 171
  • 2
  • 8
  • Any basic JPA documentation would tell you such. http://www.datanucleus.org/products/accessplatform_5_0/jpa/jpql_criteria.html – Neil Stockton Oct 28 '16 at 12:24
  • The join is in the @ManyToOne on MyDetail class, maybe I need to create another join just for the query? – gregh Oct 28 '16 at 13:37
  • if you need to refer to something other than candidate then yes you need to join ... – Neil Stockton Oct 28 '16 at 13:38
  • @ManyToOne style implicit joins won't be supported. I have loads and loads of queries using createAlias :-( – gregh Oct 28 '16 at 14:04

1 Answers1

0

You can create alias in JPA CriteriaQuery on entityRoot

Root<A> entityRoot = criteriaQuery.from(A);
entityRoot.join("b", JoinType.LEFT).alias("b");
michalavis
  • 79
  • 1
  • 8