Search code examples
hibernatecriteria

Hibernate Criteria join query


How do I create a Hibernate criteria query from the following sql?

String hql = "select e.employeeId,m.meetingId,e.firstname from Employee e join e.meetings m";

Can anyone please provide the corresponding criteria query?


Solution

  • the criteria query is:

    Criteria c = session.createCriteria(Employee.class, "e");
    c.createAlias("e.meetings", "m"); // inner join by default
    c.setProjection( Projections.distinct( Projections.projectionList()
                .add( Projections.property("e.employeeId"), "employeeId")
                .add( Projections.property("m.meetingId"), "meetingId")
                .add( Projections.property("e.firstname"), "firstname")));