如何使用 like 运算符编写 jpql 查询

how to write jpql query with like operator

如何在 java class

中用 JPQL 编写下面的 sql 查询
select e.emp_id, e.emp_name
from emp e 
left outer join dept on e.dept_id = d.dept_id and upper(d.dept_name) like upper('%dev%') 

我试过下面它对我不起作用。我需要在 like 关键字之后如何用两个百分位数和参数名称

来写
@Query(value = "select e.emp_id,e.emp_name from emp e left outer join dept on e.dept_id= d.dept_id and upper(d.dept_name)  like upper(%:deptName%), nativeQuery = true)
List<Employee> searchAllEmployees(@Param("deptName")String deptName);

假设您有以下 Employee 实体:

@Entity
public class Employee
{
   @Id
   @Column(name = "emp_id")
   Long id;

   @Column(name = "emp_name")
   String name;

   @ManyToOne
   @JoinColumn(name = "dept_id")
   Department department;
}

@Entity
public class Department
{
   @Id
   @Column(name = "dept_id")
   Long id;

   @Column(name = "dept_name")
   String name;
}

你可以编写如下jpql:

@Query("select e from Employee e left join e.department d where upper(d.name) like '%' || upper(:deptName ) || '%'")
List<Employee> searchAllEmployees(@Param("deptName")String deptName);

另请参阅休眠文档的 this 部分。