4

I am using JPA 2.2.0 in my project. I have below requirement to write a hibernate query using "CriteriaBuilder". Below is sample code snippet

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
                CriteriaQuery<A> criteriaQuery = criteriaBuilder.createQuery(A.class);
Root<A> aRoot= criteriaQuery.from(A.class);
Join<A,B> abJoin= aRoot.join("aliasB", JoinType.LEFT);

....

I want to join two tables A and B. However I want to join A with B with 2 columns c1 and c2. Is it possible in JPA 2.2.0? If so, how we can do this?

for Eg.,

I need below query,

SELECT * FROM A LEFT JOIN 
B ON A.c1 = B.c1 AND **B.c2 = 'Yes'**
Srinivasan
  • 11,718
  • 30
  • 64
  • 92
  • Defining multiple `@JoinColumns` on entity class might help: https://stackoverflow.com/questions/10845842/multi-column-join-in-hibernate-jpa-annotations#16592971 – Selaron Nov 08 '18 at 11:12
  • how to do without @JoinColumns?. Because c2 column is not foriegn key. – Srinivasan Nov 08 '18 at 11:49
  • 1
    Maybe the `.on(...)`-clause is useful in this case: https://stackoverflow.com/questions/26905047/condition-left-join-in-criteriaquery – Selaron Nov 08 '18 at 11:57
  • You don't have a join on two columns, you have a join on the FK of B.c1 and a predicate where clause on the B.c2 – K.Nicholas Nov 09 '18 at 01:48
  • 1
    Got solution from below link, [https://stackoverflow.com/questions/26905047/condition-left-join-in-criteriaquery][1] – Srinivasan Nov 09 '18 at 06:55

0 Answers0