Hibernate- QBC离线查询

package com.gordon.test;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import com.gordon.domain.Book;
import com.gordon.utils.HibernateUtil;

/**
 * QBC查询
 * 
 * @author Administrator
 */
public class TestQBCDemo2 {

	/**
	 * 离线查询 Hibernate: select this_.id as id1_0_0_, this_.name as name2_0_0_,
	 * this_.price as price3_0_0_, this_.publisher_id as publishe4_0_0_ from
	 * t_book this_ where this_.price<=? C语言程序设计 Photoshop图形图像处理
	 * VisualBasic2015实践教程 生产微服务
	 */
	@Test
	public void run() {
		/**
		 * 离线查询对象
		 */
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Book.class);
		detachedCriteria.add(Restrictions.le("price", 44.00));

		/**
		 * session
		 */
		Session currentSession = HibernateUtil.getCurrentSession();
		Transaction transaction = currentSession.beginTransaction();

		/**
		 * 设置此离线对象要被哪个session执行
		 */
		Criteria executableCriteria = detachedCriteria.getExecutableCriteria(currentSession);

		/**
		 * 查询结果
		 */
		List<Book> list = executableCriteria.list();
		for (Book book : list) {
			System.out.println(book.getName());
		}

		transaction.commit();
	}
}

 

posted @ 2017-08-24 15:03  GordonDicaprio  阅读(417)  评论(0编辑  收藏  举报