Kela's Blog

            前面的路很坎坷,但毕竟是条路.也许走过这一段就会发现,走过去就是梦想中的地方.因此坚持成为此刻唯一能做且必须去做的事情.
posts - 9, comments - 27, trackbacks - 0, articles - 15

kela的笔记 ORM产品 ---- hibernate(5)

Posted on 2005-12-08 13:29 Kela 阅读(301) 评论(0)  编辑  收藏 所属分类: 我的笔记(Hibernate)

Hibernate Query Language(HQL)

相对于Criteria提供的符合面向对象风格的查询封装模式。HQL提供了更加丰富灵活的特性,它在涵盖Criteria功能范围的前提下,提供了更加强大的查询(在Hibernate3中增加了实体的更新,删除)能力。更多的HQL使用方法请参考Hibernate开发手册和其它网络资源。HQL为官方推荐查询模式。
示例:StudentCriteriaQuery.java一个基于HQL的查询,更新,删除。
/**
 * StudentHQL.java
 * Hibernate Query Language(HQL)
 */
package com.kela.hb.model;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.kela.hb.pojo.KelaStudent;
import com.kela.hb.util.HibernateUtil;


/**
 * @author kangfeng(kela.kf@gmail.com)
 *
 */
public class StudentHQL {
 
  /*
   * 实体查询(查询数据库表中所有的纪录)
   */
  public void exampleSelect_1(){
    Session session;
    Query query;
  
    String hql;
    try {
       session = HibernateUtil.currentSession();
   
       //查询符合相应编号和年龄大于25的学生纪录
       hql = " from com.kela.hb.pojo.KelaStudent as kelaStudent " +
                " where kelaStudent.stuID = '402880fc07dfe16f0107dfe173640001' " +
                " or kelaStudent.stuAge > 25";
      query = session.createQuery(hql);
      List list = query.list();
      Iterator it = list.iterator();
      while(it.hasNext()) {
         KelaStudent kelaStudent = (KelaStudent)it.next();
         System.out.print("    姓名:" + kelaStudent.getStuName());
         System.out.print("    性别:" + kelaStudent.getStuSex());
         System.out.print("    年龄:" + kelaStudent.getStuAge());
         System.out.print("    地址:" + kelaStudent.getStuAddress());
         System.out.println();
       }
    } catch(HibernateException he) {
       System.out.println("[提示]: 查询数据发生错误," + he.getMessage());
    } finally {
       HibernateUtil.closeSession();
    }
 }
 
 /*
  * 属性查询(查询数据库表中需要的若干个字段)
  * 返回类型为数组方式
  */
 public void exampleSelect_2(){
   Session session;
   Query query;
  
   String hql;
   try {
      session = HibernateUtil.currentSession();
   
      //查询KELA_STUDENT中字段为StuName,StuAge,StuAddress的所有纪录
      //同样也可以加一些条件
      hql = " select kelaStudent.stuName, kelaStudent.stuAge, kelaStudent.stuAddress " +
               " from com.kela.hb.pojo.KelaStudent as kelaStudent ";
      query = session.createQuery(hql);
      List list = query.list();
      Iterator it = list.iterator();
      while(it.hasNext()) {
         Object[] rs = (Object[])it.next();
         System.out.print("    姓名:" + rs[0]);
         System.out.print("    年龄:" + rs[1]);
         System.out.print("    地址:" + rs[2]);
         System.out.println();
      }
    } catch(HibernateException he) {
       System.out.println("[提示]: 查询数据发生错误," + he.getMessage());
    } finally {
       HibernateUtil.closeSession();
    }
  }
 
 /*
  * 参数绑定(使用顺序占位符 ? ),相当于我们经常使用的:
  * select * from KELA_STUDENT where KelaID = ?    and KelaAge > ?
  */
  public void exampleSelect_3(){
    Session session;
    Query query;
  
    String hql;
     try {
        session = HibernateUtil.currentSession();
   
        //查询符合相应编号和年龄大于20的学生纪录
       hql = " from KelaStudent as kelaStudent " +
                " where kelaStudent.stuID = ? or kelaStudent.stuAge >= ?";
       query = session.createQuery(hql);
       //注意了是从0开始的
       query.setString(0, "402880fc07dfe16f0107dfe173640001");
       query.setInteger(1, 20);
       List list = query.list();
       Iterator it = list.iterator();
       while(it.hasNext()) {
           KelaStudent kelaStudent = (KelaStudent)it.next();
           System.out.print("    姓名:" + kelaStudent.getStuName());
           System.out.print("    性别:" + kelaStudent.getStuSex());
           System.out.print("    年龄:" + kelaStudent.getStuAge());
           System.out.print("    地址:" + kelaStudent.getStuAddress());
           System.out.println();
       }
    } catch(HibernateException he) {
       System.out.println("[提示]: 查询数据发生错误," + he.getMessage());
    } finally {
       HibernateUtil.closeSession();
    }
  }
 
  /*
   * 参数绑定(使用引用占位符 : ),相当于我们经常使用的:
   * select * from KELA_STUDENT where KelaID = ? and KelaAge > ?
   */
   public void exampleSelect_4(){
     Session session;
     Query query;
  
     String hql;
     try {
        session = HibernateUtil.currentSession();
   
         //查询符合相应编号的学生纪录
         hql = " from KelaStudent as kelaStudent " +
                  " where kelaStudent.stuID = :stuID ";
         query = session.createQuery(hql);
         //stuID 对应 :stuID
         query.setParameter("stuID", "402880fc07dfe16f0107dfe173640001");
         List list = query.list();
         Iterator it = list.iterator();
         while(it.hasNext()) {
             KelaStudent kelaStudent = (KelaStudent)it.next();
             System.out.print("    姓名:" + kelaStudent.getStuName());
             System.out.print("    性别:" + kelaStudent.getStuSex());
             System.out.print("    年龄:" + kelaStudent.getStuAge());
             System.out.print("    地址:" + kelaStudent.getStuAddress());
             System.out.println();
        }
     } catch(HibernateException he) {
         System.out.println("[提示]: 查询数据发生错误," + he.getMessage());
      } finally {
         HibernateUtil.closeSession();
      }
   }
 
   /*
    * 利用HQL的更新
    */
   public void exampleUpdate() {
     Session session;
     Transaction tran;
     Query query;
  
     String hql;
     try {

        session = HibernateUtil.currentSession();
        tran = session.beginTransaction();
   
         hql = " update KelaStudent " +
                  " set stuAge = 30 " +
                  " where stuID = '402880fc07dfe16f0107dfe173640001' ";
         query = session.createQuery(hql);
         System.out.println("更新的纪录条数是:" + query.executeUpdate());
         tran.commit();
     } catch (HibernateException he) {
         System.out.println("[提示]: 更新数据发生错误," + he.getMessage());
     } finally {
          HibernateUtil.closeSession();
      }
   }
 
  /*
   * 利用HQL的删除
   */
  public void exampleDelete() {
    Session session;
    Transaction tran;
    Query query;
  
    String hql;
    try {
        session = HibernateUtil.currentSession();
        tran = session.beginTransaction();
   
        hql = "delete KelaStudent where stuID = '402880fc080437200108043725760001'";
   
       query = session.createQuery(hql);
       System.out.println("删除纪录的条数是:" + query.executeUpdate());
        tran.commit();
     } catch(HibernateException he) {
        System.out.println("[提示]: 删除数据发生错误," + he.getMessage());
     } finally {
         HibernateUtil.closeSession();
     }
  }
  public static void main(String[] args) {
  
      StudentHQL studentHQL = new StudentHQL();
       studentHQL.exampleSelect_1();
  }

}


只有注册用户登录后才能发表评论。


网站导航:
 
分享到: