Hibernate的查询方式

Hibernate的查询方式:

1、使用HQL方法实现的查询//根据字符串查询。使用HQL方法实现的查询

public static List<User> query(String str){
                Session s = null;                
                try {
                        s = HibernateUtil.getSession();        
                        //String hql = "from User as user where user.name =?";
                        String hql = "from User as user where user.name =:name";
                        Query query = s.createQuery(hql);
                        //query.setString(0, name);
                        query.setFirstResult(0);//用这两条实现分页
                        query.setMaxResults(10);
                        query.setString("name", str);
                        List<User> list = query.list();//executeQuery();使用泛型。
                        
                        //确定只有一条满足条件的记录时可以用以下的语句。
                        //User u = (User) query.uniqueResult();
                        //System.out.println(u);
                        
                        /*for(User user:list){
                                System.out.println(user.getId());
                                System.out.println(user.getName());
                                System.out.println(user.getBirthday());
                        }*/
                        
                        return list;                        
                } finally {
                        if (s != null) {
                                s.close();
                        }
                }
        }

2、使用Criteria条件查询方式实现的查询        //Criteria条件查询方式。

public static List<User> criQuery(String name){
                Session s = null;                
                try {
                        s = HibernateUtil.getSession();        
                        Criteria c = s.createCriteria(User.class);
                        c.add(Restrictions.eq("name", name));//条件查询方法实现查询。                        
                        List<User> list = c.list();//executeQuery();使用泛型。                        
                        return list;                        
                } finally {
                        if (s != null) {
                                s.close();
                        }
                }
        }

Hibernate工具类:

/**
 * 文件名:HibernateUtil.java
 * 作  者:zqding
 * 时  间:2009-5-23
 **/
package com.starit.toolsutil;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.starit.dao.User;

public final class HibernateUtil {
        private static SessionFactory sessionFactory;
        
        //静态代码块
        static {
                Configuration cfg = new Configuration();
                cfg.configure();
                sessionFactory = cfg.buildSessionFactory();

        }

        public static SessionFactory getSessionFactory() {
                return sessionFactory;
        }

        public HibernateUtil() {

        }
        
        public static Session getSession(){
                return sessionFactory.openSession();
        }
        
        //向库表增加对象
        public static void addObject(Object entity){
                Session s = null;
                Transaction tx = null;                
                try {
                        s = HibernateUtil.getSession();
                        tx = s.beginTransaction();
                        s.save(entity);
                        tx.commit();
                
                }finally{
                        if (s != null) {
                                s.close();
                        }
                }
        }
        
        //向库表更新对象 
        public static void updateObject(Object entity){
                Session s = null;
                Transaction tx = null;                
                try {
                        s = HibernateUtil.getSession();
                        tx = s.beginTransaction();
                        s.update(entity);
                        tx.commit();
                        
                }finally{
                        if (s != null) {
                                s.close();
                        }
                }
        }
        
        //从库表删除对象
        public static void deleteObject(Object entity){
                Session s = null;
                Transaction tx = null;                
                try {
                        s = HibernateUtil.getSession();
                        tx = s.beginTransaction();
                        s.delete(entity);
                        tx.commit();
                        
                }finally{
                        if (s != null) {
                                s.close();
                        }
                }
        }
        
        //从库表根据ID查询对象数据
        public static Object getObject(Class clazz,Serializable id){
                Session s = null;
                try {
                        s = HibernateUtil.getSession();
                        Object object = s.get(clazz, id);
                        return object;
                }finally{
                        if (s != null) {
                                s.close();
                        }
                }                
        }

        
        //从库表中以HQL语句的方式查询对象
        public static List<User> getObject(String value){                
                Session s = null;                
                try {
                        s = HibernateUtil.getSession();
                        String hql = "from User as user where user.name = ?";
                        Query query = s.createQuery(hql);
                        query.setString(0, value);
                        List<User> list = query.list();//查询的记录有多个时
//                        User user = query.uniqueResult();//查询的结果中只有 一个时,可以用这个,它反回一个对象类型的。
                        return list;
                }finally{
                        if (s != null) {
                                s.close();
                        }
                }
        }
}

相关推荐