hql Criteria.add 添加条件 order by

package com.lan.Hib;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.lan.Hib.domain.User;

public class Cri {
	public static void main(String[] args) {
		cri();//("lll");
	}
	static void cri(){
		Session session = null;
		try {
			session = HibernateUtil.getSession();
			Criteria criteria = session.createCriteria(User.class);
			//添加查询条件,name相等,birthday小于new Date()
			//criteria.add(Restrictions.eq("name", name));
			criteria.add(Restrictions.lt("birthday", new Date()));
			criteria.addOrder(Order.asc("name"));
			
			List list =criteria.list();//executQuery();
			//分页查询
			criteria.setFirstResult(0);
			criteria.setMaxResults(10);
			
			//只有一个的时候才可以使用
			//User user = (User)criteria.uniqueResult();
			//System.out.println(user);
			
			for(User u : list){
				System.out.println(u.getName());
			}
		}finally{
			if(session != null){
				session.close();
			}
		}
	}
}


package com.lan.Hib;

import java.io.Serializable;

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

public final class HibernateUtil {
	private static SessionFactory sessionFactory;
	
	private HibernateUtil() {
		
	}
	
	static{
		Configuration cfg = new Configuration();
		cfg.configure();
		sessionFactory = cfg.buildSessionFactory();
	}	
	
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public static Session getSession() {
		return sessionFactory.openSession();
	}
	
	public static void add(Object entity) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			session.save(entity);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		} finally{
			if(session != null){
				session.close();
			}
		}
	}
	
	
	public static void update(Object entity) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			session.update(entity);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		} finally{
			if(session != null){
				session.close();
			}
		}
	}
	
	public static void delete(Object entity) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			session.delete(entity);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		} finally{
			if(session != null){
				session.close();
			}
		}
	}
	
	public static Object get(Class clazz,Serializable id) {
		Session session = null;
		try {
			session = HibernateUtil.getSession();
			Object object = session.get(clazz,id);
			return object;
		}finally{
			if(session != null){
				session.close();
			}
		}
	}
}


你可能感兴趣的:(黑马hibernate)