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)
上一篇 2022年9月30日 上午9:16
下一篇 2022年9月30日 上午9:16


相关推荐

  • 简历中“项目经历“该如何写?

    简历中“项目经历“该如何写?前言找工作 简历是最关键的一步 只有通过了简历筛查才能往下继续进行 很多人写简历 都是在记录流水账 看来没有任何平淡寡味 其实简历的核心价值就在于游说 hr 和面试官 看我 看我 看我 即 吸引对方的眼球 让对方翻你的牌子 所以 简历的终极目的就是在回答一个问题 为什么要选择我 而不是别人 前段时间看 剑指 Offer 1 其中看到了关于项目经验的建议 也许能解答大家的疑惑 于是本文记录一下 这本书也很推荐大家买来看看 里面都是算法题和算法思维 但它是 C 写的 所

    2026年3月19日
    2
  • JAVA错误: 找不到或无法加载主类

    JAVA错误: 找不到或无法加载主类检查环境变量配置 JAVA HOME D Java jdk1 8 0 91CLASSPATH JAVA HOME lib dt jar JAVA HOME lib tools jar 前面有英文点号 Path JAVA HOME bin JAVA HOME jre bin 设置没问题 java version 测试 显示 JDK 版本正常 我很确定自己的 JD

    2026年3月19日
    2
  • 关于三极管的理解—根据IC符号简易迅速判断三极管导通情况

    关于三极管的理解—根据IC符号简易迅速判断三极管导通情况  很不幸,开始写博客的第一天就被师兄批评了。其实很对不起师兄,当年在大学学习模拟电路的时候我不太认真,那时候天天忙着和女朋友吃吃喝喝。。所以对于三极管的各种性质与基本运用场景缺乏较深的理解,仅仅只是知道导通、截止等几种判断方式而已。今天在设计电路时涉及到了运用三极管驱动光耦器件,以及通过三极管来驱动蜂鸣器等操作,在三极管的选材和设计上出现了低级的失误。检讨完毕后,翻出当年的模电书,配…

    2022年6月17日
    44
  • 旋转机械振动的基本特性分析图_旋转机械振动监测及故障诊断

    旋转机械振动的基本特性分析图_旋转机械振动监测及故障诊断旋转机械的主要功能是由旋转部件来完成的,转子是其最主要的部件。旋转机械发生故障的主要特征是机器伴有异常的振动和噪声,其振动信号从幅域、频域和时域反映了机器的故障信息。因此,了解旋转机械在故障状态下的振动机理,对于监测机器的运行状态和提高诊断故障的准确率都非常重要。一、转子振动的基本特性旋转机械的主要部件是转子,其结构型式虽然多种多样,但对一些简单的旋转机械来说,为分析和计算方便,一般都将转

    2022年8月31日
    9
  • ionic4 ion-reorder-group组件拖拽改变item顺序

    ionic4 ion-reorder-group组件拖拽改变item顺序

    2021年6月29日
    79
  • ssdt与shadowssdt区别

    ssdt与shadowssdt区别ring3 nbsp nbsp nbsp nbsp nbsp nbsp NtOpenProces nbsp nbsp nbsp ssdt 层 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 实现 OpenProcess gt ntdll ZwOpenProces gt ntos ZwOpenProces gt ntos NtOpenProces 内核中有 2 套函数 zw nt nt 才是真正的执行函数 zw 只是一个过渡函数 可用 ida 察看 ntoskrn

    2026年3月18日
    2

发表回复

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

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