不论是nativequery还是hql的query,都可以指定需要查询的字段,只是必须定义这些字段所对应的实体,而且需要一个构造函数,构造函数的参数就是查询的字段列表。举个栗子:
@Entity @Table(name="Human") @JsonInclude(JsonInclude.Include.NON_NULL) public class Human { @Id @GeneratedValue(strategy = GenerationType.AUTO) 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; } //getter setter... }
查询人员的实体,我需要返回所有人员的名字。查询如下: