如何获取Hibernate返回的值?

时间:2015-11-19 00:05:33

标签: java mysql hibernate

我卡住了,我用hibernate运行这个查询,但不知道如何获取查询返回的值。

查询:

select idMagistrado, count(*) as numeroDemandas from demandamagistrado group by idMagistrado order by numeroDemandas asc limit 3;

DAO:

public Set<Magistrado> get3Magistrado() {
        ConnectionDataBase cdb = new ConnectionDataBase();
        Set<Magistrado> magistrados = null;

        try {
            Query createQuery = cdb.getSession().createQuery("select idMagistrado, count(*) as numeroDemandas from demandamagistrado group by idMagistrado order by numeroDemandas asc limit 3");
            // Here, HELP!
            if (magistrados == null) {
                return null;
            }
        }
        catch (HibernateException he) {
            cdb.catchException(he);
        }
        finally {
            cdb.closeSession();
        }

        return magistrados;
    }

MySQL控制台中的结果

mysql> select idMagistrado, count(*) as numeroDemandas from demandamagistrado group by idMagistrado order by numeroDemandas asc limit 3;
+--------------+----------------+
| idMagistrado | numeroDemandas |
+--------------+----------------+
|            3 |              1 |
|            2 |              2 |
|            1 |              3 |
+--------------+----------------+
3 rows in set (0.00 sec)

Query对象无法让我知道 idMagistrado numeroDemandas 的值。我不知道该怎么办,帮助。

2 个答案:

答案 0 :(得分:1)

HQL是一种面向对象的查询语言,类似于SQL,但HQL不是对表和列进行操作,而是使用持久对象及其属性。

  

让我们假设我们有一个如下所示的数据库表结构:

enter image description here

  

然后可以给出每个表的Entity类(Hibernate将Java类映射到数据库表):

<强>主题

public class Subject  implements java.io.Serializable {


 private Integer id;
 private String subName;
 private String day;
 private String time;
 private Set subjectHasStuDetailses = new HashSet();


public Subject() {
}
//getters and setters
...

<强> StuDetails

public class StuDetails  implements java.io.Serializable {


     private Integer id;
     private String FName;
     private String LName;
     private String sex;
     private String dob;
     private Set subjectHasStuDetailses = new HashSet();

    public StuDetails() {
    }
    //getters and setters
    ...

<强> SubjectHasStuDetails

public class SubjectHasStuDetails  implements java.io.Serializable {


     private Integer id;
     private StuDetails stuDetails;
     private Subject subject;

    public SubjectHasStuDetails() {
    }
    //getters and setters
    ...

在每个实体类中,我们都可以根据需要覆盖默认构造函数。

让我们假设我们的实体类位于名为 com.hibernate.entities 的包中。如果我们需要让所选学生完成所有科目,那么我们的Hql查询可以是:

Query query = ss.createQuery("FROM com.hibernate.entities.SubjectHasStuDetails shs WHERE "
                + "shs.stuDetails.id= :s_id").
                setInteger("s_id", 32);
 List<com.hibernate.entities.SubjectHasStuDetails> shs = query.list();
    for (SubjectHasStuDetails sh : shs) {
       sh.getSubject().getSubName();
    }

正如我们在Hql查询的给定示例中所看到的,我们指的是映射到所需表的类的实体成员。不引用表列名。

因此,在您的情况下,您也应该引用映射的实体。

SELECT d.member_variable, COUNT(*) AS d.member_variable FROM packagename.EntitityClass  d GROUP BY d.member_variable ORDER BY d.member_variable ASC LIMIT 3
  

<强>更新

如果您使用 已确定的关系

<强>主题:

public class Subject  implements java.io.Serializable {


     private Integer id;
     private String subName;
     private String day;
     private String time;
     private Set<Student> students = new HashSet<>();
     ...

<强>学生:

public class Student  implements java.io.Serializable {


     private Integer id;
     private String FName;
     private String LName;
     private int age;
     private Set subjects = new HashSet();
     ...

<强>查询:

Query q = s.createQuery("FROM com.hibernate.entities.Subject sub JOIN sub.students s WHERE s.id= :stu_id")
                .setInteger("stu_id", 1);
       List<String> subj = q.list();
    for (String ss : subj) {
        System.out.println(ss);
    }

答案 1 :(得分:1)

我认为你应该知道“HQL”与“SQL”不同; 当你使用HQL时,你应该从类(而不是表)查询。

所以如果你想要执行sql并获得hibernate的结果:

String sql = "select idMagistrado, count(*) as numeroDemandas from demandamagistrado         
group by idMagistrado order by numeroDemandas asc limit 3";

你应该以这种方式使用createSQLQuery(而不是createQuery)方法:

 List<Object[]> objList = cdb.getSession().createSQLQuery(sql).list();
 for(Object[] objs : objList){
    int idMagistrado = Integer.valueOf(objs[0]);
    int numeroDemandas = Integer.valuesOf(objs[1]);
    System.out.println(idMagistrado + ":" + numeroDemandas);
 }