select DISTINCT(ID), NAME, DEPARTMENT from EMPLOYEE;
我试图使用基于接口和基于类的Spring data JPA投影,但这些方法似乎都行不通。基于接口的投影给出了我无法取消代理的代理列表。在基于类的投影的构造函数中使用DISTINCT是不可能的。
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@EmbeddedId
private EmployeeKey key;
@Column(name = "NAME")
private String name;
@Column(name = "DEPARTMENT")
private String department;
@Coulmn(name = "AGE")
private Integer age;
//Getter/Setters/Constructors
}
@Embeddable
public class EmployeeKey {
@Column(name = "ID")
String id;
@Column(name = "REGNO")
String regNo;
//Getter/Setters/Constructors
}
@Repository
public interface EmployeRepository extends JpaRepository<Employee, EmployeeKey>{
@Query(value = "select distinct(emp.key.id), emp.name, emp.department from Employee emp")
List<EmployeeInterfaceProjection> findUsingInterfaceProjection();
@Query(value = "select distinct(emp.key.id) as empId, emp.name as empName, emp.department as empDepartment from Employee emp")
List<EmployeeClassProjection1> findUsingClassProjection1();
@Query(value = "select new com.path.to.EmployeeClassProjection2(emp.key, emp.name, emp.department) from Employee emp")
List<EmployeeClassProjection2> findUsingClassProjection2();
@Query(value = "select distinct(emp.key.id) as empId, emp.name as empName, emp.department as empDepartment from Employee emp")
List<Object[]> findUsingObjectProjection();
}
public interface EmployeeInterfaceProjection{
EmployeeKeyInterfaceProjection getKey();
String getName();
String getDepartment();
interface EmployeeKeyInterfaceProjection{
String getId();
}
}
public class EmployeeClassProjection1{
private String empId;
private String empName;
private String empDepartment;
//Getters/Setters, Constructors, Hashcode, Equals
}
public class EmployeeClassProjection2{
private EmployeeKey key;
private String name;
private String department;
//Getters/Setters, Constructors, Hashcode, Equals
}
每种方法所面临的问题
findUsingInterfaceProjection()
findUsingObjectProjection()
这个方法很管用,但看起来是一个很粗糙的方法。我希望使用JPA的预测。
这就是我最终成功的原因。我在查询中使用了与projection中的getter方法名匹配的列别名(alias=name,如果projection具有getName())。不需要使用内部接口EmployeeKeyInterfaceProjection。
public interface EmployeeInterfaceProjection{
String getId();
String getName();
String getDepartment();
}
@Query(value = "select distinct(emp.key.id) as id, emp.name as name, emp.department as department from Employee emp")
List<EmployeeInterfaceProjection> findUsingInterfaceProjection();
基于接口的投影在AbstractJPAQuery$TupleConverter$TupleBackedMap内部工作。另外,在不使用列别名的情况下,我为getId()、getName()、getDepartment()中的每一个都得到了null。使用别名解决了这个问题。
谢谢@Ho Wai Chan给我指明了正确的方向。
我正在使用Spring Data JPA运行MySql DB开发一个Spring MVC项目,其中有四个实体对象:旅行、费用、货币和基金。 下面是我的DB架构的可视化表示: 在ExpenseRepository接口中,我扩展了JpaRepository接口。 现在,我尝试运行一个本机SQL查询,在这里我将传递expenseId,并从费用表中获取费用和金额,从货币表中获取currency_name。
我有一个奇怪的问题,我不知道为什么会发生。我肯定我做错了什么,因为这是我第一次使用数据投影,而且我在使用DTOS时从来没有遇到过这样的问题。 我有一个SELECT statemen,它返回各种数据类型的某些列。我有一个接口,我将它传递给JPA存储库,这样它就可以进行接口映射。但是它不是根据列名映射结果(例如'accountnum'->),而是按照字母顺序映射列。因此,如果'date_of_orde
我需要有一个自定义查询的spring数据仓库方法,并希望使用基于类的投影。 看这个https://docs . spring . io/spring-data/JPA/docs/current/reference/html/# projections findDistinct运行良好。 findDistinctQuery和FindDitinctNativeQuery抛出 找不到能够从类型[org.
我不能同时使用Spring Data,JPA投影和规范。我有以下设置: 实体: 投影界面: 国家规格: 存储库: 前两个方法findByName和findAllProjectedBy工作良好。而第三个方法findAllProjectedBy(Specification Specification)抛出以下异常- 如何才能做到这一点呢?有什么想法吗?
我有一个关于嵌套列表投影界面用法的问题。我有两个实体(父和子)(它们有单向关联) 父级=> 子=> 我有两个选择特定列投影界面。 这个查询可以工作,但是它选择ChildEntity的所有列,并且只将id、name propeties映射到ChildProjection。(生成的查询选择所有列,但我想只选择id和name列) 我如何只选择id和name列(为嵌套列表投影界面选择特定列)并映射到Chi