0

I have 2 tables TABLE1 and TABLE2. hibernate class of TABLE1

class Table1 {

private Collection< table2s> table2s= new ArrayList< table2s>();

}

Now using criteria I need to fetch Table1 records and the results should have Table2s one records among list which is linked to Table1.

eg: TABLE 1 TABLE1ID NAME 1 A 2 B TABLE 2 TABLE2ID TABLE1ID TABLE2NAME 1 1 XXXXX 2 1 XXXXXX 3 1 XXX 4 2 YYYY 5 2 YYYYYY

Expected Output: TABLE1ID NAME TABLE2ID TABLE2NAME 1 A 1 XXXXX 2 B 4 YYYY

something like table1.gettable2s().get(0)

Shashank V C
  • 153
  • 1
  • 1
  • 9

1 Answers1

0

use join() method

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Table1> q = cb.createQuery(Table1.class);
    Root<Table1> r = q.from(Table2.class);

    Join<Table2, Table1> usersJoin = r.join("field", JoinType.LEFT);
Peter Šály
  • 2,848
  • 2
  • 12
  • 26