Hibernate JPQL查询排序不直接在可分页排序值上取列名 @Query(“选择新的com.user.dto.response.EmployeeDetails(e.empName、e.empCode、e.address、e.locationCode、ul.userName,” +“ul.isLoginEnabled,ul.role)” +“来自员工电子邮件\r\n” +“左加入用户登录ul\r\n” +“在e.empCode=ul.empCode上\r\n” +“其中(e.locationCode=:locationCode)和(:role为NULL或e.role类似%:role%)\r\n” +“和(:isLoginEnabled为NULL或ul.isLoginEnabled=:isLoginEnabled)\r\n”) Page getEmployeeDetails(@Param(“locationCode”)字符串locationCode,@Param(“role”)字符串角色, @参数(“isLoginEnabled”)布尔值isLoginEnabled,Pageable Pageable); @AllargsConstructor//lombok 类别雇员详情{ 字符串名称; 字符串编码; 字符串地址; 字符串位置码; 字符串用户名; 布尔型isLoginEnabled; 字符串角色; }

Hibernate JPQL查询排序不直接在可分页排序值上取列名 @Query(“选择新的com.user.dto.response.EmployeeDetails(e.empName、e.empCode、e.address、e.locationCode、ul.userName,” +“ul.isLoginEnabled,ul.role)” +“来自员工电子邮件\r\n” +“左加入用户登录ul\r\n” +“在e.empCode=ul.empCode上\r\n” +“其中(e.locationCode=:locationCode)和(:role为NULL或e.role类似%:role%)\r\n” +“和(:isLoginEnabled为NULL或ul.isLoginEnabled=:isLoginEnabled)\r\n”) Page getEmployeeDetails(@Param(“locationCode”)字符串locationCode,@Param(“role”)字符串角色, @参数(“isLoginEnabled”)布尔值isLoginEnabled,Pageable Pageable); @AllargsConstructor//lombok 类别雇员详情{ 字符串名称; 字符串编码; 字符串地址; 字符串位置码; 字符串用户名; 布尔型isLoginEnabled; 字符串角色; },hibernate,spring-boot,jpa,jpql,Hibernate,Spring Boot,Jpa,Jpql,所以,当我经过的时候 >在?sort=empCode%2Cdesc中,在查询中它显示为“e.empCode”desc >在?sort=role%2Cdesc中,在查询中它显示为“e.role”desc 这就是为什么它显示错误,因为“e.role”不在查询中 但是,如果我提供 >在?sort=u.role%2Cdesc中,在查询中它显示为“u.role”desc并正在工作 因此,当我公开API时,UI将只知道empCode、empName、isLoginEnabled、role等 他们应该如何知道

所以,当我经过的时候

>在?sort=empCode%2Cdesc中,在查询中它显示为“e.empCode”desc

>在?sort=role%2Cdesc中,在查询中它显示为“e.role”desc 这就是为什么它显示错误,因为“e.role”不在查询中

但是,如果我提供 >在?sort=u.role%2Cdesc中,在查询中它显示为“u.role”desc并正在工作

因此,当我公开API时,UI将只知道empCode、empName、isLoginEnabled、role等

他们应该如何知道何时通过“ul.”作为前缀或不作为前缀

有什么办法吗


我试图用alias给所有构造函数赋值,但它仍然没有考虑。它总是调用“e.colName asc/desc”

您可以在查询
中使用
作为角色。。新软件包。员工详细信息(…,ul.role as role)

注意:它应该是
Page getEmployeeDetails
而不是
Page getEmployeeDetails

使用Kotlin的简化示例

@Query("SELECT new com.user.dto.response.EmployeeDetails(e.empName, e.empCode, e.address, e.locationCode,ul.userName, "
        + " ul.isLoginEnabled, ul.role) "
        + " FROM Employee e \r\n"
        + " LEFT JOIN UserLogin ul \r\n"
        + "     ON e.empCode = ul.empCode \r\n"
        + " WHERE (e.locationCode = :locationCode) AND AND (:role IS NULL OR e.role LIKE %:role%) \r\n"
        + " AND (:isLoginEnabled IS NULL OR ul.isLoginEnabled = :isLoginEnabled) \r\n")
Page<EmployeeDetails> getEmployeeDetails(@Param("locationCode") String locationCode, @Param("role") String role,
        @Param("isLoginEnabled") Boolean isLoginEnabled, Pageable pageable);


@AllargsConstructor  //lombok
class EmployeeDetails{
    String empName;
    String empCode;
    String address;
    String locationCode;
    String userName;
    Boolean isLoginEnabled;
    String role;
}
@实体数据类用户(@Id val account:String,val name:String)
@实体数据类电子邮件(@Id val Email:String,@manytone val user:user)
数据类UserEmail(val名称:字符串,val电子邮件:字符串)
接口UserRepo:PagingAndSortingRepository
接口EmailRepo:PagingAndSortingRepository{
//在查询中使用“e.user.name作为名称”,这允许我们
//像/emails?sort=name,asc而不是/emails?sort=user.name,asc这样的请求
@查询(“从电子邮件e中选择new so.demo.UserEmail(e.user.name作为名称,e.email))
fun findAllUserEmail(可分页:可分页):第页
}
@RestController
类ApiController(private val userRepo:userRepo,private val emailRepo:emailRepo){
@GetMapping(“/emails”)
全部乐趣(可分页:可分页)=emailRepo.findAllUserEmail(可分页)
}
示例代码可在

@Query(“选择e”)上找到
+“来自员工e”
+“左加入用户登录ul”
+“在e.empCode=ul.emp_代码上”
+“其中(e.location\u code=:locationCode)和(:role为NULL或e.role LIKE%:role%)”
+“AND(:is_login_enabled为NULL或ul.is_login_enabled=:isLoginEnabled)”,nativeQuery=true)
Page getEmployeeDetails(@Param(“locationCode”)字符串locationCode,@Param(“role”)字符串角色,
@参数(“isLoginEnabled”)布尔值isLoginEnabled,Pageable Pageable);
@AllargsConstructor//lombok
类别雇员详情{
@列(name=“emp_name”)
字符串名称;
@列(name=“emp\U代码”)
字符串编码;
@列(name=“address”)
字符串地址;
@列(name=“location\u code”)
字符串位置码;
@列(name=“user\u name”)
字符串用户名;
@列(name=“是否已启用登录”)
布尔型isLoginEnabled;
@列(name=“role”)
字符串角色;

上面的代码可能会有所帮助。

我尝试了'as'仍然在调用e.role。我在查询中提到,我尝试了'alias'
Page getEmployeeDetails
Page getEmployeeDetails
?很抱歉,这是EmployeeDetails,我将其复制到了伪变量,在那里我错过了github上使用别名shoull的示例d workScneario-1.当我在Join“e.empName as name”中为回购的同一实体重命名allias时,称为“name,asc”,它抛出“找不到类型名称的属性角色码”。它只捕获同一存储库中字段的名称。我已在员工存储库中写入。因此,员工DAO的所有字段都在工作。对于“ul.IsLogineEnabled”,“ul.isLoginEnabled,desc”它认为“找不到类型名称的属性ul”,对于“ul.isLoginEnabled as isLogin”,“isLogin,desc”它抛出“找不到类型名称的属性isLogin”,这段代码与我的代码有什么不同?为什么编写本机查询?
@Entity data class User(@Id val account : String, val name : String)
@Entity data class Email(@Id val email : String, @ManyToOne val user : User)

data class UserEmail(val name:String, val email:String)

interface UserRepo : PagingAndSortingRepository<User, String>

interface EmailRepo : PagingAndSortingRepository<Email, String> {

    //use 'e.user.name as name' in query,  this allows us to make
    //requests like /emails?sort=name,asc instead of  /emails?sort=user.name,asc 
    @Query("select new so.demo.UserEmail( e.user.name as name, e.email) from Email e" )
    fun findAllUserEmail(pageable: Pageable) : Page<UserEmail>
}

@RestController
class ApiController (private val userRepo : UserRepo, private val emailRepo: EmailRepo){
    @GetMapping("/emails")
    fun all(pageable: Pageable) = emailRepo.findAllUserEmail(pageable)
}
@Query("SELECT e "
        + " FROM Employee e "
        + " LEFT JOIN UserLogin ul "
        + "     ON e.empCode = ul.emp_code "
        + " WHERE (e.location_code = :locationCode) AND (:role IS NULL OR e.role LIKE %:role%) "
        + " AND (:is_login_enabled IS NULL OR ul.is_login_enabled = :isLoginEnabled)", nativeQuery = true)
Page<EmployeeDetails> getEmployeeDetails(@Param("locationCode") String locationCode, @Param("role") String role,
        @Param("isLoginEnabled") Boolean isLoginEnabled, Pageable pageable);




@AllargsConstructor  //lombok
class EmployeeDetails{
    @Column(name = "emp_name")
    String empName;

    @Column(name = "emp_code")
    String empCode;

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

    @Column(name = "location_code")
    String locationCode;

    @Column(name = "user_name")
    String userName;

    @Column(name = "is_login_enabled")
    Boolean isLoginEnabled;

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