Spring数据jpa,jparepository返回字符串列表代替DTO对象

时间:2016-08-05 21:24:03

标签: java spring jpa spring-data spring-data-jpa

我有interface实施 JPARepository 并有三种方法,其中一种方法是自定义@Query

public interface PersonRepository extends JpaRepository<Person, Long> {

  List<Person> getPersonBycountryCode(String countryCode);

  List<Person> findByCountryCodeAndCity(String string,String city);

  @Query(value = "SELECT person.firstName as firstName, person.lastName as lastName, person.countryCode as country, person.city as city,"
              + " SQRT(POWER((69.1 * (person.age - :age )) , 2 )"
              + " + POWER((53 * (person.experience - :experience )), 2)) as eligibility"
              + " FROM Person person"
              + " ORDER BY eligibility ASC")
  List<PersonDetailsDto> findPersonDetailsByEligibility(
          @Param("age") BigDecimal age,
          @Param("experience") BigDecimal experience,
          Pageable pageable
  );
}

问题是: @Query方法未返回PersonDetailsDto列表但返回字符串列表列表( List<List<String>> )。

PersonDetailsDto是一个POJO类,其中包含查询输出(firstName,lastName,country,city,eligibility)中描述的所有变量,以及包含所有变量作为参数的构造函数。其他两个方法确实返回Person对象列表。

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

实际上JpaRepository<Person, Long>意味着,您只能在jpa存储库方法中使用Person作为您的dto。

对于您的解决方案,您只需在存储库中定义dto接口:

public interface PersonRepository extends JpaRepository<Person, Long> {

  List<Person> getPersonBycountryCode(String countryCode);

  List<Person> findByCountryCodeAndCity(String string,String city);

  @Query(value = "SELECT person.firstName as firstName, person.lastName as lastName, person.countryCode as country, person.city as city,"
              + " SQRT(POWER((69.1 * (person.age - :age )) , 2 )"
              + " + POWER((53 * (person.experience - :experience )), 2)) as eligibility"
              + " FROM Person person"
              + " ORDER BY eligibility ASC")
  List<PersonDetailsDto> findPersonDetailsByEligibility(
          @Param("age") BigDecimal age,
          @Param("experience") BigDecimal experience,
          Pageable pageable
  );

 //define the interface here
 public interface PersonDetailsDto{
   public String getFirstName();
   public String getLastName();
   public String getCountry();
   public String getCity();
   public Integer getEligibility();
 }

}

答案 1 :(得分:0)

如果我没有错,JPA背后没有寻找特定字段的想法是成本(效率明智)相同,从表格的一行带来一列或所有列。但是为了解决您的问题,您可以设置{ {1}}来自Repository类的nativeQuery = true注释,如下所示:

@Query

我希望这可以帮助您解决问题。

答案 2 :(得分:0)

您可以在查询@Query时使用new关键字。并确保您拥有PersonDetailsDto的相应构造函数,并更改包名称。

@Query(value = "SELECT new com.company.PersonDetailsDto(person.firstName, person.lastName, person.countryCode , person.city ,"
          + " SQRT(POWER((69.1 * (person.age - :age )) , 2 )"
          + " + POWER((53 * (person.experience - :experience )), 2)) "
          + " FROM Person person"
          + " ORDER BY eligibility ASC")
List<PersonDetailsDto> findPersonDetailsByEligibility(
      @Param("age") BigDecimal age,
      @Param("experience") BigDecimal experience,
      Pageable pageable
);

类似question's answer

答案 3 :(得分:0)

只是通过别名来称呼它,它对我来说就是这样 例如:

@Query(value = "SELECT person FROM Person person"
          + " ORDER BY eligibility ASC")
List<PersonDetailsDto> findPersonDetailsByEligibility(
      @Param("age") BigDecimal age,
      @Param("experience") BigDecimal experience,
      Pageable pageable
);