Java 没有构造函数的Spring数据JPA类投影

Java 没有构造函数的Spring数据JPA类投影,java,hibernate,jpa,spring-data-jpa,Java,Hibernate,Jpa,Spring Data Jpa,我可以有这样一个界面投影: public interface Person { String getFirstName(); String getLastName(); } public interface EmployeeRepository extends JpaRepository<Employee, Long> { @Query("select e.firstName as firstName, e.lastName as lastName from

我可以有这样一个界面投影:

public interface Person {
  String getFirstName();
  String getLastName();
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

  @Query("select e.firstName as firstName, e.lastName as lastName from Employee e")
  List<Person> fetchEmployeeNames();
}
public class Person {
  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  // getters and setters
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

  @Query("select new my.package.Person(e.firstName, e.lastName) from Employee e")
  List<Person> fetchEmployeeNames();
}
我可以在
JpaRepository
中使用JPQL查询,如下所示:

public interface Person {
  String getFirstName();
  String getLastName();
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

  @Query("select e.firstName as firstName, e.lastName as lastName from Employee e")
  List<Person> fetchEmployeeNames();
}
public class Person {
  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  // getters and setters
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

  @Query("select new my.package.Person(e.firstName, e.lastName) from Employee e")
  List<Person> fetchEmployeeNames();
}
对于使用如下构造函数的JPQL:

public interface Person {
  String getFirstName();
  String getLastName();
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

  @Query("select e.firstName as firstName, e.lastName as lastName from Employee e")
  List<Person> fetchEmployeeNames();
}
public class Person {
  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  // getters and setters
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

  @Query("select new my.package.Person(e.firstName, e.lastName) from Employee e")
  List<Person> fetchEmployeeNames();
}

为什么我不能在不指定构造函数的情况下使用类投影,就像接口JPQL一样?是否允许在不指定构造函数的情况下进行类投影?我可以创建一个通用转换器来转换为任何类投影吗?

您尝试的是不可能的。如果您需要自定义WHERE条件,您可以通过接口中的默认方法使用
findAll(规范)
方法来实现这一点。

您尝试的是不可能的。如果您需要自定义WHERE条件,您可以通过接口中的默认方法使用
findAll(规范s)
方法来实现这一点。

只需在存储库中编写
List findBy()我想这会满足您的要求,但是如果我想在查询中使用where条件、连接或其他东西呢?我想继续在复杂的查询中使用JPQL,您可以使用where条件来完成这项工作,但最终会得到很长的方法名,而且只适用于where条件。那么连接、分组方式和函数调用等呢?如果您有太多复杂的查询,最好使用带有类或接口的构造函数。只需在存储库
List findBy()中编写我想这会满足您的要求,但是如果我想在查询中使用where条件、连接或其他东西呢?我想继续在复杂的查询中使用JPQL,您可以使用where条件来完成这项工作,但最终会得到很长的方法名,而且只适用于where条件。连接、分组方式和函数调用等呢?如果有太多复杂的查询,最好将构造函数与类或接口一起使用。