2

Code for join Query

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<SchoolTO> cquery = builder.createQuery(SchoolTO.class);
Root<SchoolTO> root = cquery.from(SchoolTO.class);
Join<SchoolTO, StudentTO> join = root.join(SchoolTO_.studentList);  
cquery.select(root);
cquery.where(builder.equal(join.get(StudentTO_.name),  "john"));
cquery.distinct(true);
List<School> list = session.createQuery(cquery).getResultList();

School class structure

public class School
{
  private List<Student> list;
}

Student class structure

public class Student
{
  private School school;
} 

Data

  • School1 -> John, Mathew, William, Megan, Daisy
  • School2 -> John,William, Samantha
  • School3 -> Daisy, Mathew

Actual Result from Join Hibernate code

  • School1 -> John, Mathew, William, Megan, Daisy
  • School2 -> John, William, Samantha

Expected Result

  • School1 -> John
  • School2 -> John

The hibernate join gets me all schools with student name "john". But when I access the list of student objects inside school object it has students of all names. I am trying to achieve the one under expected result. Let me know what I am wrongly doing here. Thanks in advance

0 Answers0