0
Select * from Table A left join Table B on A.id=B.id and A.langCode='IN';

where 'IN' is input from user.

Table A and Table B has Mapping For Id but there is no Mapping with LangCode between the two as table B dosent have an column called langCode to map with , i want to write the following query using hibernate criteria without mapping langcode.

Table: Employee : EMP_ID - primary key , NAME , CONTACT_DETAILS

Table:Employee_Lang: EMP_ID- composite primary key, LANG_CODE- composite primary key, NAME

Actual Query:

Select * from Employee Emp left outer join Employee_Lang EmpLang on Emp.EMP_ID=EmpLang.EMP_ID AND EmpLang.LANG_CODE='IN'

I have mapped only the Emp_Id as primary key from both the tables in hibernate hence hibernate criteria will only apply a join on that And not on LangCode.

Note:-I cant change hibernate mapping and can use only hibernate Criteria , as per the clients requirement, please help me on this one.

1 Answers1

0

For example you have two models:

@Entity
class Employee {
  @Id
  private Long id;
  @OneToOne
  private EmployeeLang employeeLang;
  ...
}
@Entity
class EmployeeLang {
  @Id
  private Long id;
  private String langCode;
  @OneToOne
  private Employee employee;
  ...
}

You should have some mapping between them and then you can do next criteria:

Criteria criteria = session.createCriteria(Employee.class);
criteria.createAlias("employeeLang", "empLang"); // inner join with EmployeeLang
criteria.add(Restrictions.eq("empLang.langCode", "in");

List<Employee> employees = criteria.list();
Orest
  • 6,548
  • 10
  • 54
  • 84