JPA 投影查询不带其他独立字段的字段列表 [英] JPA projection query not brining List of fields with other standalone fields

查看:46
本文介绍了JPA 投影查询不带其他独立字段的字段列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了许多链接,例如:Spring JPA 选择特定列,但是这是我的模型.我正在使用 Spring Boot 2.0.0

I already went through many links like : Spring JPA selecting specific columns, but here is my model. I am using Spring Boot 2.0.0

@Entity
@Table
public class Employee{
   @Id
   private Long employeeId;
   ....
   ....
   @OneToMany(mappedBy = "departmentId", cascade = CascadeType.ALL)
   private List<Department> departments;
   ..
   ...
   ...
   ...
   ...
   ...
}

我写过类似的项目

public class EmployeeDto{
    public Long getEmployeeId();

    public List<Department> getDepartments();
}

当我开发 JPARepositoryQuery 时,我得到了整个对象.事实上,我只需要EmployeeId 和departmentIds 列表就可以了.

When I developed JPARepositoryQuery, I get whole object. In fact, I am only expecting EmployeeId and list of departmentIds thats it.

@Query("SELECT s.employeeId, s.departments FROM Employee s WHERE s.employeeId = :employeeId")
EmployeeDto findProjectionByEmployeeId(@Param("employeeId") Long employeeId);

我们如何限制只能获取两个字段?

How can we restrict to get only two fields?

推荐答案

答案很简单:那是不可能的.

The answer is short: That's not possible.

您的查询结果将是一个带有 id 和 Department 的所有字段的笛卡尔积.

The result of your query will be a cartesian product with id and all fields of Department.

这篇关于JPA 投影查询不带其他独立字段的字段列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆