`

Hibernate HQL 查询

 
阅读更多

 

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateQLTest {
	private static SessionFactory sf;
	
	@BeforeClass
	public static void beforeClass() {
		sf = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	@AfterClass
	public static void afterClass() {
		sf.close();
	}
	
	@Test
	public void testSchemaExport() {
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
	}
	
	@Test
	public void testSave() {
		Session session = sf.openSession();
		session.beginTransaction();
		
		for(int i=0; i<10; i++) {
			Category c = new Category();
			c.setName("c" + i);
			session.save(c);
		}
		
		for(int i=0; i<10; i++) {
			Category c = new Category();
			c.setId(1);
			Topic t = new Topic();
			t.setCategory(c);
			t.setTitle("t" + i);
			t.setCreateDate(new Date());
			session.save(t);
			
		}
		
		for(int i=0; i<10; i++) {
			
			Topic t = new Topic();
			t.setId(1);
			Msg m = new Msg();
			m.setCont("m" + i);
			m.setTopic(t);
			session.save(m);
			
		}
		
		
		
		
		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();
		
	}
	
	 @Test
	public void testQBE() {
		Session session = sf.openSession();
		session.beginTransaction();
		Topic tExample = new Topic();
		tExample.setTitle("T_");
		
		Example e = Example.create(tExample)
					.ignoreCase().enableLike();
		Criteria c = session.createCriteria(Topic.class)
					 .add(Restrictions.gt("id", 2))
					 .add(Restrictions.lt("id", 8))
					 .add(e)
					 ;
					 
		
		for(Object o : c.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getId() + "-" + t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
		
	}
	//is empty and is not empty
	@Test
	public void testQBC() {
		Session session = sf.openSession();
		session.beginTransaction();
		//criterion 标准/准则/约束
		Criteria c = session.createCriteria(Topic.class) //from Topic
					 
					 .add(Restrictions.gt("id", 2)) //greater than = id > 2
					 .add(Restrictions.lt("id", 8)) //little than = id < 8
					 .add(Restrictions.like("title", "t_"))
					 .createCriteria("category")
					 .add(Restrictions.between("id", 3, 5)) //category.id >= 3 and category.id <=5
					 ;
		//DetachedCriterea
		for(Object o : c.list()) {
			Topic t = (Topic)o;
			System.out.println(t.getId() + "-" + t.getTitle());
		}
		session.getTransaction().commit();
		session.close();
		
	}
	
	public static void main(String[] args) {
		beforeClass();
	}
}

 

分享到:
评论

相关推荐

    Hibernate hql查询语法总结

    《Hibernate HQL查询语法详解》 Hibernate,作为Java领域中广泛应用的对象关系映射(ORM)框架,极大地简化了数据库操作。其内置的HQL(Hibernate Query Language)是一种面向对象的查询语言,它允许开发者以类和...

    Hibernate HQL查询 分页查询 模糊查询.docx

    这里的关键点在于使用`createQuery`方法创建了一个HQL查询对象,并通过`setString`方法设置参数值,最后通过`list`方法执行查询并获取结果列表。 #### 三、分页查询 分页查询在处理大量数据时非常有用,可以有效地...

    Hibernate HQL查询语句总结.docx

    Hibernate HQL 查询语句总结 Hibernate HQL 查询语句是 Hibernate 框架中的一种查询语言,它提供了更加丰富的和灵活的查询特性,具有类似标准 SQL 语句的查询方式,同时也提供了更加面向对象的封装。以下是 ...

    Hibernate HQL查询.docx

    ### Hibernate HQL 查询详解 #### 一、HQL 概述 Hibernate 提供了一种非常强大的查询语言——HQL(Hibernate Query Language),它在语法上与 SQL 类似但功能更为强大,因为它支持面向对象的特性,例如继承、多态性...

    hibernate HQL 查询

    hibernate HQL 查询教程 管理 Session ThreadLocal 接口的使用

    hibernateHQL关联查询

    ### Hibernate HQL 关联查询详解 #### 一、引言 在进行数据库操作时,关联查询是必不可少的一部分,尤其在处理复杂的数据结构时更是如此。Hibernate作为一种流行的Java持久层框架,提供了强大的对象关系映射(ORM...

    Hibernate hql查询

    **Hibernate HQL查询** Hibernate Query Language(HQL)是Hibernate框架提供的一种面向对象的查询语言,它是基于Java的,但其语法与SQL类似,用于在Java应用中操作对象。HQL允许开发者用类名和属性名而不是表名和...

    HibernateHQL.zip_更新

    HQL查询是Hibernate的核心功能之一,它能让我们以类和对象的方式来查询数据。基本语法如下: ```java Query query = session.createQuery("from EntityName where property = ?"); query.setParameter(0, value); ...

    Hibernate-HQL-查询-Query资料

    本资料主要涵盖了Hibernate HQL查询的基本概念、语法以及实际应用。 1. **HQL概述** - HQL是Hibernate提供的面向对象的查询语言,与SQL类似,但更加面向对象,将数据库表映射为Java类,表中的行映射为类的对象。 ...

    Hibernate HQL基础练习小列子+数据库文件

    Hibernate HQL基础练习小列子+数据库

    Hibernate查询语言HQL.PPT

    Hibernate 查询语言 HQL Hibernate 查询语言 HQL 是一种面向对象的查询语言,用于检索对象。它提供了灵活多样的查询机制,包括导航对象图、通过标识符检索、使用 HQL 检索对象、使用 Hibernate 条件 API 和使用本地...

    hibernate hql各类查询范例

    以上内容涉及了使用 HQL 和 DetachedCriteria 进行查询、参数化查询、执行更新操作以及简单的统计查询等常见的 Hibernate 操作。这些知识点对于理解和掌握 Hibernate 框架的基本使用至关重要,同时也是开发基于 ...

    hibernateHQL基本查询

    ### Hibernate HQL 基本查询详解 #### 一、HQL 概述 HQL (Hibernate Query Language) 是一种面向对象的查询语言,由 Hibernate 官方提供,用于简化数据库查询过程。与传统的 JDBC 方式相比,使用 HQL 进行数据库...

    hibernate 查询?Hibernate的HQL查询

    【标题】:深入理解Hibernate的查询机制:HQL查询 【描述】:在ORM框架Hibernate中,数据查询和检索是一个核心功能,它提供了多种查询方式,包括标准化对象查询(Criteria Query)、Hibernate查询语言(HQL)和原生...

    Hibernate_HQL.rar_hibernate HQL_hibernate hql src

    对hibernate的hql进行了详尽的讲解

    Hibernate-HQL.rar_HQL_hibernate hql

    在Hibernate中,通过Session对象的createQuery或createSQLQuery方法创建HQL查询,然后调用list、uniqueResult、scroll等方法执行查询。执行过程中,Hibernate会自动进行类型转换和结果集的封装,极大地降低了开发...

    Hibernate HQL.txt

    下面是一个简单的Java程序示例,展示了如何在Java中使用Hibernate框架执行HQL查询: ```java import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate....

    HibernateHql综合测试小程序

    本程序“HibernateHql综合测试小程序”是针对Hibernate中的HQL(Hibernate Query Language)进行的一系列功能测试,涵盖了二十多种不同的HQL语句,旨在帮助开发者深入理解和熟练运用Hibernate的查询能力。...

    Hibernate HQL 结合 velocity

    1. **执行HQL查询**:在Java代码中,使用`Session`对象的`createQuery()`或`createNativeQuery()`方法创建HQL查询,然后调用`list()`或`uniqueResult()`等方法获取查询结果。 2. **处理查询结果**:将查询结果转换...

Global site tag (gtag.js) - Google Analytics