Hiberante HQL

	@Test
	public void testHQL_01() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from Category");
		List<Category> categorys = (List<Category>) q.list();
		for (Category c : categorys) {
			System.out.println(c.getId() + "--" + c.getName());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_02() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from Category c where c.name > 'c5'");
		List<Category> categorys = (List<Category>) q.list();
		for (Category c : categorys) {
			System.out.println(c.getId() + "--" + c.getName());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_03() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from Category c order by c.name desc");
		List<Category> categorys = (List<Category>) q.list();
		for (Category c : categorys) {
			System.out.println(c.getId() + "--" + c.getName());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_04() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("select distinct c from Category c order by c.name desc");
		List<Category> categorys = (List<Category>) q.list();
		for (Category c : categorys) {
			System.out.println(c.getId() + "--" + c.getName());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_05() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();

		Query q = session
				.createQuery("from Category c where c.id > :min and c.id < :max");
		q.setParameter("min", 3);
		q.setParameter("max", 8);

		List<Category> categorys = (List<Category>) q.list();
		for (Category c : categorys) {
			System.out.println(c.getId() + "--" + c.getName());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_06() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();

		Query q = session
				.createQuery("from Category c where c.id > ? and c.id < ?");
		q.setParameter(0, 3);
		q.setParameter(1, 8);

		List<Category> categorys = (List<Category>) q.list();
		for (Category c : categorys) {
			System.out.println(c.getId() + "--" + c.getName());
		}
		session.getTransaction().commit();
	}

	/**
	 * 分页显示
	 */
	@Test
	public void testHQL_07() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session
				.createQuery("select distinct c from Category c order by c.name desc");
		q.setMaxResults(3);
		q.setFirstResult(1);
		List<Category> categorys = (List<Category>) q.list();
		for (Category c : categorys) {
			System.out.println(c.getId() + "--" + c.getName());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_08() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session
				.createQuery("select c.id , c.name from Category c order by c.name desc");

		List<Object[]> categorys = (List<Object[]>) q.list();
		for (Object b[] : categorys) {
			System.out.println(b[0] + "--" + b[1]);
		}
		session.getTransaction().commit();
	}

	/**
	 * 版块id=1的所有帖子
	 */
	@Test
	public void testHQL_09() {

		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session
				.createQuery("from Msg m where m.Topic.Category.id = 1");

		List<Object[]> categorys = (List<Object[]>) q.list();
		for (Object b[] : categorys) {
			System.out.println(b[0] + "--" + b[1]);
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_10() {
		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session
				.createQuery("select new chengyi.hibernate.MsgInfo(m.id,m.count,"
						+ "m.topic.title,m.topic.category.name) from Msg");
		for (Object o : q.list()) {
			MsgInfo m = (MsgInfo) o;
			System.out.println(m.getCount());
		}

		session.getTransaction().commit();
	}

	// 动手测试left right join
	// 为什么不能直接写Category名,而必须写t.category
	// 因为有可能存在多个成员变量(同一个类),需要指明用哪一个成员变量的连接条件来做连接
	@Test
	public void testHQL_11() {
		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session
				.createQuery("select t.title, c.name from Topic t join t.category c");

		for (Object o : q.list()) {
			Object ob[] = (Object[]) o;
			System.out.println(ob[0] + "-" + ob[1]);
		}

		session.getTransaction().commit();
	}

	/**
	 * count(*) 返回的就只有一个Long类型的结果
	 */
	@Test
	public void testHQL_12() {
		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("select count(*) from Msg m");

		Long count = (Long) q.uniqueResult();
		System.out.println(count);
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_13() {
		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from Msg m where m.id between 3 and 5");

		for (Object o : q.list()) {
			Msg m = (Msg) o;
			System.out.println(m.getId() + "-" + m.getCount());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_14() {
		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from Msg m where m.id in (3,4, 5)");

		for (Object o : q.list()) {
			Msg m = (Msg) o;
			System.out.println(m.getId() + "-" + m.getCount());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_15() {
		testSave();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from Msg m where m.count is not null");

		for (Object o : q.list()) {
			Msg m = (Msg) o;
			System.out.println(m.getId() + "-" + m.getCount());
		}
		session.getTransaction().commit();
	}

	@Test
	public void testHQL_16() {
		testSave();
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session
				.createQuery("select max(m.id), min(m.id), avg(m.id), sum(m.id) from Msg m");

		Object[] o = (Object[]) q.uniqueResult();
		System.out.println(o[0] + "-" + o[1] + "-" + o[2] + "-" + o[3]);
		session.getTransaction().commit();
		session.close();
	}
	
	//is empty and is not empty
	@Test
	public void testHQL_20() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Topic t where t.msgs is empty");
		
		for(Object o : q.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getId() + "-" + t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_21() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Topic t where t.title like '%5'");
		
		for(Object o : q.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getId() + "-" + t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_22() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Topic t where t.title like '_5'");
		
		for(Object o : q.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getId() + "-" + t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
		
	}
	//不重要
	@Test
	public void testHQL_23() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("select lower(t.title)," +
											 "upper(t.title)," +
											 "trim(t.title)," +
											 "concat(t.title, '***')," +
											 "length(t.title)" +
											 " from Topic t ");
		
		for(Object o : q.list()) {
			Object[] arr = (Object[])o;
			System.out.println(arr[0] + "-" + arr[1] + "-" + arr[2] + "-" + arr[3] + "-" + arr[4] + "-");
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_24() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("select abs(t.id)," +
											 "sqrt(t.id)," +
											 "mod(t.id, 2)" +
											 " from Topic t ");
		
		for(Object o : q.list()) {
			Object[] arr = (Object[])o;
			System.out.println(arr[0] + "-" + arr[1] + "-" + arr[2] );
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_25() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("select current_date, current_time, current_timestamp, t.id from Topic t");
		
		for(Object o : q.list()) {
			Object[] arr = (Object[])o;
			System.out.println(arr[0] + " | " + arr[1] + " | " + arr[2] + " | " + arr[3]);
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_26() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Topic t where t.createDate < :date");
		q.setParameter("date", new Date());
		for(Object o : q.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_27() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("select t.title, count(*) from Topic t group by t.title") ;
		for(Object o : q.list()) {
			Object[] arr = (Object[])o;
			System.out.println(arr[0] + "|" + arr[1]);
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_28() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("select t.title, count(*) from Topic t group by t.title having count(*) >= 1") ;
		for(Object o : q.list()) {
			Object[] arr = (Object[])o;
			System.out.println(arr[0] + "|" + arr[1]);
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_29() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Topic t where t.id < (select avg(t.id) from Topic t)") ;
		for(Object o : q.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void testHQL_30() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Topic t where t.id < ALL (select t.id from Topic t where mod(t.id, 2)= 0) ") ;
		for(Object o : q.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
	}
	
	//用in 可以实现exists的功能
	//但是exists执行效率高
	@Test
	public void testHQL_31() {
		Session session = sf.openSession();
		session.beginTransaction();// t.id not in (1)
		Query q = session.createQuery("from Topic t where not exists (select m.id from Msg m where m.topic.id=t.id)") ;
//		Query q = session.createQuery("from Topic t where exists (select m.id from Msg m where m.topic.id=t.id)") ;
		for(Object o : q.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
	}
	
	//update and delete
	//规范并没有说明是不是要更新persistent object,所以如果要使用,建议在单独的trasaction中执行
	
	@Test
	public void testHQL_32() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("update Topic t set t.title = upper(t.title)") ;
		
		q.executeUpdate();
		q = session.createQuery("from Topic");
		for(Object o : q.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getTitle());
		}
		session.createQuery("update Topic t set t.title = lower(t.title)")
			.executeUpdate();
		session.getTransaction().commit();
		session.close();
		
	}
	
	//不重要
	@Test
	public void testHQL_33() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.getNamedQuery("topic.selectCertainTopic");
		q.setParameter("id", 5);
		Topic t = (Topic)q.uniqueResult();
		System.out.println(t.getTitle());
		session.getTransaction().commit();
		session.close();
	}
	
	//Native(了解)
	@Test
	public void testHQL_34() {
		Session session = sf.openSession();
		session.beginTransaction();
		SQLQuery q = session.createSQLQuery("select * from category limit 2,4").addEntity(Category.class);
		List<Category> categories = (List<Category>)q.list();
		for(Category c : categories) {
			System.out.println(c.getName());
		}
		session.getTransaction().commit();
		session.close();
		
	}

你可能感兴趣的:(Hiberante HQL)