Rhythmk 学习 Hibernate 09 - Hibernate HQL

1、初始数据

@Test
	public void test01() {
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();

			session.beginTransaction();

			for (int i = 0; i < 100; i++) {
				book entity = new book();
				entity.setBookCode("book-code-" + i);
				entity.setBookname("book-name" + i);
				entity.setPrice(i);
				session.save(entity);
			}
			session.getTransaction().commit();
		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

  

2、查询对象

@Test
	public void test02() {
		Session session = null;
		// form 对象名 非 表名
		String sql = " from book ";
		try {
			session = HibernateUtil.getSessionFactory().openSession();

			Query query = session.createQuery(sql);
			List<book> list = (List<book>) query.list();

			for (book b : list) {
				System.out.println(b);
			}

		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

  

3、查询单字段

@Test
	public void test03() {
		/*
		 * 查询单字段
		 */
		Session session = null;

		String sql = "  select bookname from book ";
		try {
			session = HibernateUtil.getSessionFactory().openSession();

			Query query = session.createQuery(sql);
			List<String> list = (List<String>) query.list();

			for (String b : list) {
				System.out.println(b);
			}

		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

  

4、查询指定字段

	@Test
	public void test04() {
		/*
		 * 查询多字段,且指定条数Object[]
		 */
		Session session = null;

		String sql = "  select   bookname,bookCode from book ";
		try {
			session = HibernateUtil.getSessionFactory().openSession();

			Query query = session.createQuery(sql);
			// 指定5条
			query.setMaxResults(5);
			List<Object[]> list = (List<Object[]>) query.list();

			for (Object[] b : list) {
				System.out.println(b[0] + "," + b[1]);

			}

		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

  

5、返回Map对象

@Test
	public void test05() {
		/*
		 * 查询多字段,且指定Map
		 */
		Session session = null;

		String sql = "  select   new Map(bookname,bookCode) from book ";
		try {
			session = HibernateUtil.getSessionFactory().openSession();

			Query query = session.createQuery(sql);

			query.setMaxResults(5);
			List<Map> list = (List<Map>) query.list();

			for (Map b : list) {

				System.out.println((String) b.get("0") + ","
						+ (String) b.get("1"));
			}

		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

  

6、序列化参数

   6.1、

@Test
	public void test06() {

		Session session = null;

		String sql = "  select   bookname,bookCode from book   where book_id>:book_id and price>:price";
		try {
			session = HibernateUtil.getSessionFactory().openSession();

			Query query = session.createQuery(sql);
			query.setInteger("book_id", 5);
			query.setDouble("price", 5);
			// Hibernate: select book0_.bookname as col_0_0_, book0_.bookCode as
			// col_1_0_ from t_book book0_ where book0_.book_id>? and
			// book0_.price>?
			List<Object[]> list = (List<Object[]>) query.list();

			for (Object[] b : list) {
				System.out.println(b[0] + "," + b[1]);
			}

		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

  

   6.2、

@Test
	public void test07() {

		Session session = null;

		String sql = "  select   bookname,bookCode from book   where book_id>:book_id and price>:price";
		try {
			session = HibernateUtil.getSessionFactory().openSession();

			Query query = session.createQuery(sql);
			query.setInteger(0, 5);
			query.setDouble(1, 5);
			// Hibernate: select book0_.bookname as col_0_0_, book0_.bookCode as
			// col_1_0_ from t_book book0_ where book0_.book_id>? and
			// book0_.price>?
			List<Object[]> list = (List<Object[]>) query.list();

			for (Object[] b : list) {
				System.out.println(b[0] + "," + b[1]);
			}

		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

  

7、执行SQL

@Test
	public void test08_jdbc() {
		Session session = null;
//Rhythmk 
		String sql = "  select bookCode,bookname from t_book  where  book_id>? and price>?";
		try {
			session = HibernateUtil.getSessionFactory().openSession();

			SQLQuery query = session.createSQLQuery(sql);
			query.setInteger(0, 5);
			query.setDouble(1, 5);

			List<Object[]> list = (List<Object[]>) query.list();
			for (Object[] b : list) {
				System.out.println(b[0] + "," + b[1]);
			}

		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

}

  http://pan.baidu.com/s/1hqGDNq0

 

posted @ 2014-05-11 18:22  Rhythmk  阅读(249)  评论(0编辑  收藏  举报
Rhythmk 个人笔记