简体   繁体   English

如何在休眠条件中编写以下查询

[英]How Can I write The Following Query In hibernate Criteria

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

where 'IN' is input from user. 用户输入“ IN”的地方。

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. 表A和表B都有ID映射,但是在两者之间没有使用LangCode映射,因为表B有一列名为langCode的映射,我想使用休眠条件编写以下查询而不映射langcode。

Table: Employee : EMP_ID - primary key , NAME , CONTACT_DETAILS 表:员工:EMP_ID-主键,NAME,CONTACT_DETAILS

Table:Employee_Lang: EMP_ID- composite primary key, LANG_CODE- composite primary key, NAME 表:Employee_Lang:EMP_ID-复合主键,LANG_CODE-复合主键,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. 我只将Emp_Id映射为hibernate中两个表中的主键,因此,休眠条件将仅对该字段应用联接,而不对LangCode进行联接。

Note:-I cant change hibernate mapping and can use only hibernate Criteria , as per the clients requirement, please help me on this one. 注意:-我无法更改休眠映射,并且只能根据客户要求使用休眠条件,请对此提供帮助。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM