spring-data-jpa只查询实体部分字段的情况

不论是nativequery还是hql的query,都可以指定需要查询的字段,只是必须定义这些字段所对应的实体,而且需要一个构造函数,构造函数的参数就是查询的字段列表。举个栗子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
@Entity  
@Table(name="tcHuman")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Human {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "tableKeyGenerator")
@TableGenerator(name = "tableKeyGenerator", table = "tcTableKeyGenerator",
pkColumnName = "pk_key", valueColumnName = "pk_value", pkColumnValue = "humanID",
initialValue = 1, allocationSize = 1)
private Integer humanID;

private String humanName;
//人员代码
private String humanCode;
private String humanPassword;
private String description;
//所属单位
private Integer unitID;
private Integer displayOrder;
private Integer identifyType;
private Integer activeFlag;

public Human() {
}

public Human(String humanName) {
this.humanName = humanName;
}

public Integer getHumanID() {
return humanID;
}

public void setHumanID(Integer humanID) {
this.humanID = humanID;
}

public String getHumanName() {
return humanName;
}

public void setHumanName(String humanName) {
this.humanName = humanName;
}

public String getHumanCode() {
return humanCode;
}

public void setHumanCode(String humanCode) {
this.humanCode = humanCode;
}

public String getHumanPassword() {
return humanPassword;
}

public void setHumanPassword(String humanPassword) {
this.humanPassword = humanPassword;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Integer getUnitID() {
return unitID;
}

public void setUnitID(Integer unitID) {
this.unitID = unitID;
}

public Integer getDisplayOrder() {
return displayOrder;
}

public void setDisplayOrder(Integer displayOrder) {
this.displayOrder = displayOrder;
}

public Integer getIdentifyType() {
return identifyType;
}

public void setIdentifyType(Integer identifyType) {
this.identifyType = identifyType;
}

public Integer getActiveFlag() {
return activeFlag;
}

public void setActiveFlag(Integer activeFlag) {
this.activeFlag = activeFlag;
}
}

查询人员的实体,我需要返回所有人员的名字。查询如下:

1
2
@Query("select new Human (h.humanName) from Human  h ")  
List<Human> getHumanList();

我们需要查询humanName,所以必须要有public Human(String humanName)这个构造函数,而且必须提供默认的构造函数,否则entity无法构造。

这样虽然方便,但是如果返回的字段偏多,那么这个构造函数就参数列表就很长。这种情况最好还是用nativequery,定义的接口其返回类型为List

对于Object[]返回结果的处理如果想做成通用的,可以参考下GSON的反序列化,通过传递T.class,通过反射去构造对象并设置字段。

坚持原创,看官随意。
分享本文
0%