Created
April 16, 2018 12:57
-
-
Save ThangLeQuoc/3d769c092f5e07596fbb666b9c0bb9a9 to your computer and use it in GitHub Desktop.
Criteria API - Join non relational tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SELECT a FROM A a, B b WHERE a.someField = b.otherField ORDER BY b.anotherField | |
CriteriaBuilder cb = emf.getCriteriaBuilder(); | |
CriteriaQuery<A> crit = cb.createQuery(A.class); | |
Root<A> aRoot = crit.from(A.class); | |
Root<B> bRoot = crit.from(B.class); | |
aRoot.alias("a"); | |
bRoot.alias("b"); | |
crit.select(aRoot); | |
Predicate fieldEquals = cb.equal(aRoot.get(A_.someField), bRoot.get(B_.otherField); | |
crit.where(fieldEquals); | |
crit.orderBy(cb.asc(bRoot.get(B_.anotherField))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment