Hibernate查询特定几个字段

时间:2021-09-29 15:30:13

Hibernate查询特定几个字段

1.    问题场景

考试系统开发中,类似考题、答题卡等对象,可能包含如“题目内容”、“答案及评语”等信息量非常大的字段,而实际使用中,列表显示不需要查询所有的对象属性,这时若把所有属性全部取出来太耗费资源,因此查询特定的几个字段就可以。

2.    实现方法

说明:实现方法可能有很多,这里只列出亲测有效的两种:

1.通过hql语句实现,分别获取字段数据并手动组装成对象

Question.java

package com.geariot.platform.exam.entities;

 

import java.util.Date;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Table;

 

import org.hibernate.annotations.Cache;

import org.hibernate.annotations.CacheConcurrencyStrategy;

import org.hibernate.annotations.GenericGenerator;

 

@Entity

@Table(name="question")

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="javaClassName")

public class Question {

 

         private String id;

         private int level;  //难度

         private int type;   //类别,单选题0,多选题1,填空题2,问答题3

         private String typeName; //类别名称

         private String abs;//题目缩略内容

         private String content;//题目内容

         private String subject;//科目名称

         private String subjectId;//科目Id

         private Date createTime;//创建时间

         private String answer; //标准答案

        

         @Id

         @Column(length=32)

         @GeneratedValue(generator="system-uuid")

         @GenericGenerator(name="system-uuid", strategy="uuid")

         public String getId() {

                   return id;

         }

         public void setId(String id) {

                   this.id = id;

         }

        

         public int getLevel() {

                   return level;

         }

         public void setLevel(int level) {

                   this.level = level;

         }

         public int getType() {

                   return type;

         }

         public void setType(int type) {

                   this.type = type;

         }

        

         @Column(name = "type_name")

         public String getTypeName() {

                   return typeName;

         }

         public void setTypeName(String typeName) {

                   this.typeName = typeName;

         }

         public String getContent() {

                   return content;

         }

         public void setContent(String content) {

                   this.content = content;

         }

         public String getSubject() {

                   return subject;

         }

         public void setSubject(String subject) {

                   this.subject = subject;

         }

        

         @Column(name = "subject_id")

         public String getSubjectId() {

                   return subjectId;

         }

         public void setSubjectId(String subjectId) {

                   this.subjectId = subjectId;

         }

        

         @Column(name = "create_time")

         public Date getCreateTime() {

                   return createTime;

         }

         public void setCreateTime(Date createTime) {

                   this.createTime = createTime;

         }

         public String getAnswer() {

                   return answer;

         }

         public void setAnswer(String answer) {

                   this.answer = answer;

         }

        

         public String getAbs() {

                   return abs;

         }

         public void setAbs(String abs) {

                   this.abs = abs;

         }

        

         @Override

         public String toString() {

                   return "Question [id=" + id + ", level=" + level + ", type=" + type

                                     + ", typeName=" + typeName + ", abs=" + abs + ", content="

                                     + content + ", subject=" + subject + ", subjectId=" + subjectId

                                     + ", createTime=" + createTime + ", answer=" + answer + "]";

         }

}

 

QuestionDaoImpl.java(附复合查询)

public List<Question> getQuestions(Question q, int start, int number) {

                   StringBuffer basicQueryStr ;

                   basicQueryStr =  new StringBuffer("select id, level, type, typeName, abs, subject from Question");

                   QuestionAndQueryCreator creator = new QuestionAndQueryCreator(q);

                   String creatorStr = creator.createStatement();

                   Query query = QueryUtils.createLocalQuery(getSession(),basicQueryStr, creatorStr,ORDER_CON.DESC_ORDER);

                   query.setCacheable(true);

                   query.setFirstResult(start);//设置起始行

                   query.setMaxResults(number);//每页条数  

                  

                   List<Question> list = new ArrayList<Question>();

                   List<Object[]> resultList = query.list();

                   for(Object[] obj:resultList)

                   {

                            Question que = new Question();

                           

                            que.setId((String)obj[0]);

                            que.setLevel((int)obj[1]);

                            que.setType((int)obj[2]);

                            que.setTypeName((String)obj[3]);

                            que.setAbs((String)obj[4]);

                            que.setSubject((String)obj[5]);

                           

                            list.add(que);

                   }

                   return query.list();

         }

浏览器调用接口(省略)返回json结果:

{"data":[["abc321",0,1,"多选","题目","历史"]],"code":"0","msg":"成功","size":1}

发现返回的”data”对象少了属性名,只有属性值,这样不太复合要求。

2. 通过HQL语句new POJO()实现

Question.java

package com.geariot.platform.exam.entities;

 

import java.util.Date;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Table;

 

import org.hibernate.annotations.Cache;

import org.hibernate.annotations.CacheConcurrencyStrategy;

import org.hibernate.annotations.GenericGenerator;

 

@Entity

@Table(name="question")

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="javaClassName")

public class Question {

 

         //因为"select new Question(id, level, type, typeName, abs, subject) from Question"

         //所以必须创建带有待查询参数的构造方法和空的构造方法

         public Question(String id, int level, int type, String typeName,

                            String abs, String subject) {

                   this.id = id;

                   this.level = level;

                   this.type = type;

                   this.typeName = typeName;

                   this.abs = abs;

                   this.subject = subject;

         }

         public Question()

         {

                  

         }

         private String id;

         private int level;  //难度

         private int type;   //类别,单选题0,多选题1,填空题2,问答题3

         private String typeName; //类别名称

         private String abs;//题目缩略内容

         private String content;//题目内容

         private String subject;//科目名称

         private String subjectId;//科目Id

         private Date createTime;//创建时间

         private String answer; //标准答案

        

         @Id

         @Column(length=32)

         @GeneratedValue(generator="system-uuid")

         @GenericGenerator(name="system-uuid", strategy="uuid")

         public String getId() {

                   return id;

         }

         public void setId(String id) {

                   this.id = id;

         }

        

         public int getLevel() {

                   return level;

         }

         public void setLevel(int level) {

                   this.level = level;

         }

         public int getType() {

                   return type;

         }

         public void setType(int type) {

                   this.type = type;

         }

        

         @Column(name = "type_name")

         public String getTypeName() {

                   return typeName;

         }

         public void setTypeName(String typeName) {

                   this.typeName = typeName;

         }

         public String getContent() {

                   return content;

         }

         public void setContent(String content) {

                   this.content = content;

         }

         public String getSubject() {

                   return subject;

         }

         public void setSubject(String subject) {

                   this.subject = subject;

         }

        

         @Column(name = "subject_id")

         public String getSubjectId() {

                   return subjectId;

         }

         public void setSubjectId(String subjectId) {

                   this.subjectId = subjectId;

         }

        

         @Column(name = "create_time")

         public Date getCreateTime() {

                   return createTime;

         }

         public void setCreateTime(Date createTime) {

                   this.createTime = createTime;

         }

         public String getAnswer() {

                   return answer;

         }

         public void setAnswer(String answer) {

                   this.answer = answer;

         }

        

         public String getAbs() {

                   return abs;

         }

         public void setAbs(String abs) {

                   this.abs = abs;

         }

        

         @Override

         public String toString() {

                   return "Question [id=" + id + ", level=" + level + ", type=" + type

                                     + ", typeName=" + typeName + ", abs=" + abs + ", content="

                                     + content + ", subject=" + subject + ", subjectId=" + subjectId

                                     + ", createTime=" + createTime + ", answer=" + answer + "]";

         }

        

        

        

}

QuestionDaoImpl.java(附复合查询)

public List<Question> getQuestions(Question q, int start, int number) {

                   StringBuffer basicQueryStr ;

                   basicQueryStr =  new StringBuffer("select new Question(id, level, type, typeName, abs, subject) from Question");

                   QuestionAndQueryCreator creator = new QuestionAndQueryCreator(q);

                   String creatorStr = creator.createStatement();

                   Query query = QueryUtils.createLocalQuery(getSession(),basicQueryStr, creatorStr,ORDER_CON.DESC_ORDER);

                   query.setCacheable(true);

                   query.setFirstResult(start);//设置起始行

                   query.setMaxResults(number);//每页条数  

                  

                   return query.list();

         }

接口调用(省略)json返回结果

{"data":[{"typeName":"多选","id":"abc321","abs":"题目","level":0,"subject":"历史","type":1}],"code":"0","msg":"成功","size":1}