简体   繁体   English

使用 HQL 从子表中获取记录时出现不区分大小写的问题

[英]case insensitive issue while fetching records from child table using HQL

I have a parent table and a child table where I am only getting 1 record from child table but not getting case insensitive matched record which is a mixed string.我有一个父表和一个子表,其中我只从子表中获取 1 条记录,但没有获取不区分大小写的匹配记录,这是一个混合字符串。 I am expecting it should return 2 records.我期望它应该返回 2 条记录。

Below is the code for the same.下面是相同的代码。

//parent Table
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

  @Id
  @Column(name = "employeeID")
  private String employeeID;

  @Column(name = "name_first")
  private String nameFirst;

  @Column(name = "name_last")
  private String nameLast;

}

//Child Table
@Entity
@Table(name = "employee_salary")
public class EmployeeSalary implements Serializable {

  @EmbeddedId
  private EmployeeSalaryPK employeeSalaryPKCompositeKey;

  @Column(name = "salaryBracket")
  private String salaryBracket;

}

@Embeddable
public class EmployeeSalaryPK implements Serializable {

  @Column(name = "employeeID")
  private String employeeID;

  @Column(name = "salary")
  private String salary;

}

In employee_salary table I have two records (as shown below) but while fetching it using HQL only one record is coming with an actual match but case insensitive record is not coming.在employee_salary 表中,我有两条记录(如下所示),但是在使用HQL 获取它时,只有一条记录带有实际匹配但不区分大小写的记录。

Employee Record:- ABC John Kramer员工记录:- ABC John Kramer

employee_salary table record:- employee_salary 表记录:-

ABC 100900 ABC 100900

aBc 76770 ABC 76770

I am using simple HQL query (see below code) but getting only first record whenever I want to get both record as employeeID is abc.我正在使用简单的 HQL 查询(见下面的代码),但只要我想同时获取两条记录,因为employeeID 是 abc,就只获取第一条记录。

String hqlQuery = " FROM " + Employee.class.getName() + " E WHERE E.employeeID= :EMPLOYEEID";
Session session = entityManager.unwrap(Session.class);
List<?> responseList = session.createQuery(hqlQuery).setParameter("EMPLOYEEID", "ABC").list();

To get all entities by case insensetive String id you have to convert id to same case ( lowercase or uppercase ) on both sides of the WHERE clause equality operator要通过不区分大小写的字符串 id 获取所有实体,您必须在WHERE 子句相等运算符的两侧将 id 转换为相同的大小写( lowercaseuppercase

String hqlQuery = " FROM " + Employee.class.getName() + " E WHERE lower(E.employeeID) = :EMPLOYEEID";
Session session = entityManager.unwrap(Session.class);
List<?> responseList = session.createQuery(hqlQuery).setParameter("EMPLOYEEID", "ABC".toLowerCase()).list();

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

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