java Spring 数据 jpa,jparepository 返回字符串列表代替 DTO 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38797950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring data jpa, jparepository returning list of string in place of DTO object
提问by Ashutosh Gupta
I have a interface
implementing JPARepository
and have three methods, one of them is having a custom @Query
.
我有一个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
);
}
Problem is:method with @Query
does not return list of PersonDetailsDto
but return list of list of strings (List<List<String>>
).
问题是:方法 with@Query
不返回列表,PersonDetailsDto
但返回字符串列表列表 ( List<List<String>>
)。
PersonDetailsDto
is a POJO class with all the variables described in a query output (firstName, lastName, country, city, eligibility)and also a constructor with all the variables as Parameters. Other two methods does return list of Person object.
PersonDetailsDto
是一个 POJO 类,其中包含查询输出中描述的所有变量(名字、姓氏、国家、城市、资格),也是一个将所有变量作为参数的构造函数。其他两个方法确实返回 Person 对象的列表。
Any idea?
任何的想法?
回答by atul ranjan
Actually JpaRepository<Person, Long>
means that, you can use only Person as your dto in jpa repository methods.
实际上JpaRepository<Person, Long>
意味着,您只能在 jpa 存储库方法中使用 Person 作为您的 dto。
For your solution you can just define your dto interface inside the repository :
对于您的解决方案,您只需在存储库中定义 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();
}
}
回答by ichak khoury
just call it by its alias, it worked for me like that ex :
只需按别名称呼它,它就对我有用,例如:
@Query(value = "SELECT person FROM Person person"
+ " ORDER BY eligibility ASC")
List<PersonDetailsDto> findPersonDetailsByEligibility(
@Param("age") BigDecimal age,
@Param("experience") BigDecimal experience,
Pageable pageable
);
回答by Grinish Nepal
If I am not wrong the idea behind JPA not looking for specific fields is that is cost (efficiency wise) the same to bring one column or all columns from one row of the table.But to solve your problem you can set nativeQuery = true
in the @Query
annotation from a Repository class like this:
如果我没有错的话,JPA 背后的想法是不寻找特定字段的成本(效率方面)与从表的一行中引入一列或所有列的成本相同。但是要解决您的问题,您可以nativeQuery = true
在@Query
注释中进行设置像这样的 Repository 类:
public static final String FIND_SOMETHING = "SELECT somethingId, somethingName FROM something";
@Query(FIND_SOMETHING, nativeQuery = true)
public List<Object[]> findSomethings();
I hope this will help you to resolve your problem.
我希望这将帮助您解决您的问题。
回答by prem kumar
You can use new keyword in query of @Query. And make sure you have the appropriate constructor for PersonDetailsDto and also change package name.
您可以在@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
);
Similar question's answer.
类似问题的回答。