如何在HIbernate中聚合函数

时间:2019-04-03 21:08:51

标签: java hibernate spring-mvc

我在使用Spring MVC框架的休眠模式下使用“从Patient p中选择max(pid)”,它显示的是“ Stacktrace:],其根本原因是 org.hibernate.hql.internal.ast.QuerySyntaxException:患者未映射”以及更多错误。为什么????

<%
Configuration cfg = new Configuration();
cfg.configure("doc.cfg.xml");

SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();

Query q = s.createQuery("select max(pid) from Patient p");
List lst = q.list();  
%>

实际上我想要值     <%= lst%> 并希望从我的POJO类Patient中获取pid的最大值,该数据库的pid为整数值。

Patient.java    包dao;

公立班级患者{

private String ppass , pname , mob , age , sex  , addr;
private int pid;
public String getPpass() {
    return ppass;
}
public void setPpass(String ppass) {
    this.ppass = ppass;
}
public String getPname() {
    return pname;
}
public void setPname(String pname) {
    this.pname = pname;
}
public String getMob() {
    return mob;
}
public void setMob(String mob) {
    this.mob = mob;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}
public String getSex() {
    return sex;
}
public void setSex(String sex) {
    this.sex = sex;
}
public String getAddr() {
    return addr;
}
public void setAddr(String addr) {
    this.addr = addr;
}
public int getPid() {
    return pid;
}
public void setPid(int pid) {
    this.pid = pid;
}


}

doc.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 <hibernate-configuration>  
 <session-factory>
 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name= 
  "connection.url">jdbc:mysql://localhost:3306/doctoapp</property>
 <property name="connection.username">root</property>

  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hbm2ddl.auto">update</property>
  <property name="show_sql">true</property>

   <!-- student class ka mapping --> 
   <mapping resource="doc.hbm.xml"/>   
   <mapping resource="pat.hbm.xml"/>



   </session-factory>
   </hibernate-configuration>

0 个答案:

没有答案