hibernate createquery_executequery方法出错

hibernate createquery_executequery方法出错/** *添加 */ publicvoidsave(Stustu){   try{    tran=this.GetSession().beginTransaction();    this.GetSession().save(stu);    tran.commit();   }catch(HibernateExceptione){

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

 

/** 

* 添加 

*/ 

public void save(Stu stu){ 

   try { 

    tran=this.GetSession().beginTransaction(); 

    this.GetSession().save(stu); 

    tran.commit(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

/** 

* 使用HQL全查询 

*/ 

public List getallbyHQL(){ 

   List arr=null; 

   try { 

    String hql=”from Stu”; 

    Query query=this.GetSession().createQuery(hql); 

    arr=query.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 



   /** 

* 根据主键查询 

*/ 

public Stu getbyID(int id){ 

   Stu stu=null; 

   try { 

    stu=(Stu) this.GetSession().get(Stu.class, id); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return stu; 

/** 

* 根据对象属性查询(使用Query) 

*/ 

public List getbyPropertyQuery(String name){ 

   List arr=null; 

   try { 

    //这里不能像SQL语一样select * from Stu where SName=:name,这是不对的。 

//    Query query=this.GetSession().createQuery(“from Stu where SName=:name”); 

//    query.setString(“name”, name); 

    //或者 

    Query query=this.GetSession().createQuery(“from Stu where SName=?”); 

    query.setString(0, name); 

    arr=query.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 

/** 

* 根据对象属性查询(使用Criteria) 

*/ 

public List getbyPropertyCriteria(String name){ 

   List arr=null; 

   try { 

    Criteria cri=this.GetSession().createCriteria(Stu.class); 

    Criterion c1=Expression.eq(“SName”, name); 

    cri.add(c1); 

    arr=cri.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 

/** 

* 查询部分属性 

*/ 

public List getProperty(){ 

   List arr=new ArrayList(); 

   try { 

    String hql=”select s.SName,s.SSex from Stu as s”; 

    Query query=this.GetSession().createQuery(hql); 

    List list=query.list(); 

    Iterator iter=list.iterator(); 

    while(iter.hasNext()){ 

     Object[] obj=(Object[]) iter.next(); 

     Stu s=new Stu(); 

     s.setSName(obj[0].toString()); 

     s.setSSex(obj[1].toString()); 

     arr.add(s); 

    } 

   } catch (HibernateException e) { 

    this.CloseSession(); 

   } 

   return arr; 



/** 

* 查询一个属性 

*/ 

public List getoneProperty(){ 

   List arr=new ArrayList(); 

   try { 

    String hql=”select s.SName from Stu as s”; 

    Query query=this.GetSession().createQuery(hql); 

    Iterator iter=query.iterate(); 

    while(iter.hasNext()){ 

     Object obj=(Object) iter.next(); 

     Stu s=new Stu(); 

     s.setSName(obj.toString()); 

     arr.add(s); 

    } 

   } catch (HibernateException e) { 

    this.CloseSession(); 

   } 

   return arr; 

/** 

*查询一个对象一个属性值 

*/ 

public Object getonlyProprotyValue(int s_id){ 

   Object obj=null; 

   try { 

    String hql=”select s.SName from Stu as s where s.SId=?”; 

    Query query=this.GetSession().createQuery(hql); 

    query.setInteger(0, s_id); 

    obj=query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return obj; 



/** 

* SQL查询 

*/ 

public List getallBYSQL(){ 

   List arr=null; 

   try { 

    String sql=”select {c.*} from stu as c”; 

    SQLQuery sqlquery=this.GetSession().createSQLQuery(sql); 

    sqlquery.addEntity(“c”,Stu.class); 

    arr=sqlquery.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 

/** 

* 根据对象查询 

*/ 

public List getallByObject(Stu s){ 

   List arr=null; 

   try { 

    String hql=”from Stu as s where s=:stuentity”; 

    //或者 

    //String hql=”from Stu as s where s.SId=:stuentity”; 

    Query query=this.GetSession().createQuery(hql); 

    query.setEntity(“stuentity”, s); 

    arr=query.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   }  

   return arr; 

/** 

* 模糊查询 

*/ 

public List getallQueryLike(String name){ 

   List arr=null; 

   try { 

    String hql=”from Stu as s where s.SName like :name”; 

    Query query=this.GetSession().createQuery(hql); 

    query.setString(“name”, “%”+name+”%”); 

    //不能 

    //query.setString(“name”, “‘%”+name+”%'”); 

    arr=query.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 



   /** 

* 统计函数 

*/ 

public int CountStu(){ 

   int count=0; 

   try { 

    String hql=”select count(*) from Stu”; 

    Query query=this.GetSession().createQuery(hql); 

    count=(Integer) query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return count; 

   /** 

* 条件统计 

*/ 

public int CountByWhere(String sex){ 

   int count=0; 

   try { 

    Query query=this.GetSession().createQuery(“select count(*) from Stu where SSex=:sex”); 

    query.setString(“sex”, sex); 

    count=(Integer)query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return count; 

/** 

* 统计平均值 

*/ 

public float VagAge(){ 

   float vag=0; 

   try { 

    Query query=this.GetSession().createQuery(“select avg(SAge) from Stu”); 

    vag=(Float)query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return vag; 

/** 

* 求和函数 

*/ 

public int sumage(){ 

   int sum=0; 

   try { 

    Query query=this.GetSession().createQuery(“select sum(SAge) from Stu”); 

    sum=(Integer)query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return sum; 

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/187574.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • etl算法详解_数据拉链处理什么意思

    etl算法详解_数据拉链处理什么意思所谓拉链,就是记录历史。记录一个事物从开始,一直到当前状态的所有变化的信息。   在历史表中对客户的一生的记录可能就这样几条记录,避免了按每一天记录客户状态造成的海量存储的问题:(NAME)人名(START-DATE)开始日期(END-DT)结束日期(STAT)状态    client             19000101                       

    2022年10月16日
    3
  • windows下的pycharm配置 linux环境

    windows下的pycharm配置 linux环境由于最近学习python的需要,为了方便程序的调试,尝试在Windows下的Pycharm远程连接到虚拟机中Centos下的python环境。(这里我采用的是ssh的远程连接)1、准备工作:固定ce

    2022年7月6日
    24
  • 系统调用(int 0x80)详解

    系统调用(int 0x80)详解1、系统调用初始化在系统启动时,会在sched_init(void)函数中调用set_system_gate(0x80,&system_call),设置中断向量号0x80的中断描述符:#defineset_system_gate(n,addr) _set_gate(&idt[n],15,3,addr)其中15表示此中断号对应的是陷阱门,注意,这个中断向量不是中断门描述符

    2025年11月6日
    2
  • Redis命令——集合(Set)[通俗易懂]

    Redis命令——集合(Set)[通俗易懂]Redis命令——集合(Set)

    2022年4月20日
    47
  • list集合转化为数组_list集合转成数组

    list集合转化为数组_list集合转成数组packagecom.loaderman.jdk5;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;publicclassDemo4_AsList{/***数组转换成集合*数组转换成集合虽然不能增加或减少元素,但是可以用集合的思想操作数组,也就是说可以使用其他集合中的方法*/public…

    2022年9月19日
    2
  • TFS2010中文版安装「建议收藏」

    TFS2010中文版安装「建议收藏」 VS2010的中文版出来一段时间了,对TFS2010的了解,也有一段时间了,只不过中文版还是首次见到。于是把第一次安装的图片分享出来,公供参数。TFS2010安装环境是操作系统为WindowsServer2003SP2(X86),WindowsServer2003R2(X86),WindowsServer2003R2SP2(X86),WindowsServer200…

    2022年9月23日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号