0

I have a table called programs and I want to create a query using CriteriaBuilder that joins the table to itself as in:

SELECT *
FROM programs p1
LEFT JOIN programs p2 
    ON p1.name = p2.name AND p1.version < p2.version 

So far I have

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Program> cq = cb.createQuery(Program.class);
Root<Program> root = cq.from(Program.class);
Join<Program, Program> programJoin = root.join("name", JoinType.LEFT);
programJoin.on(cb.equal(//I don't know...))

But I'm stuck here, not knowing how to recreate the above SQL ON condition p1.version < p2.version

Mike S
  • 1,451
  • 1
  • 16
  • 34
  • 1
    The duplicate in question does not answer my question, as it does not deal with a scenario in which both tables are of the same type – Mike S Mar 22 '19 at 20:11
  • Did you try the answer of the other question? What error do you get? – dur Mar 23 '19 at 15:00
  • I'm afraid this is not possible using criteria builder because criteria builder works with your entities model. You would need to have the association to another Program declared in your Program class. Your `root.join("name", JoinType.LEFT)` won't work neither because it expects the "name" to be property of type Program but it is String. If you used type-safe metamodel (instead of String names of properties) you would get compiler error. – bambula Mar 23 '19 at 18:44

0 Answers0