Partial selection of Spring Data JPA
When we use select
to retrive some data from MySQL, we do not want to get the whole row with all columns.
For example, I have the following tab_staff_info
table with nearly 100 columns:
1 |
|
In most cases, I only want to get some (not all) of the columns. For example, I want to get the basic information, which are: companyId
, staffNo
, staffName
and idCardNo
.
In order to do this, I need to modify the StaffInfoDao
interface.
1 | public interface StaffInfoDao extends JpaRepository<StaffInfo, String> { |
And of couse we need to add a constructor to support new StaffInfo(s.companyId, s.staffNo, s.staffName, s.idCardNo)
in the JPA QL:
1 | public StaffInfo(String companyId, String staffNo, String staffName, String idCardNo) { |
Change log level of hibernate library to debug and we can see the following output:
1 | [20160808 14:30:14][DEBUG][org.hibernate.SQL](SqlStatementLogger.java:109) - select staffinfo0_.companyId as col_0_0_, staffinfo0_.staffNo as col_1_0_, staffinfo0_.staffName as col_2_0_, staffinfo0_.idCardNo as col_3_0_ from tab_staff_info staffinfo0_ where staffinfo0_.companyId=? |
转载请注明出处:Partial selection of Spring Data JPA
原文地址:https://www.xiaotanzhu.com/spring/2016-08-10-partial-selection-with-jpa.html