NativeSQL >HQL.> EJBQL(JPQL 1.0) > QBC(Query By Criteria) > QBE(Query By Example)
@Test
public void testHQL_01() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category");//相当于select c from Category c;
List<Category> categories = q.list();
for (Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_02() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c where c.name > 'c5'");
List<Category> categories = q.list();
for (Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_03() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c order by c.name desc");
List<Category> categories = q.list();
for (Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_04() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select distinct c from Category c order by c.name desc");
List<Category> categories = q.list();
for (Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_05() {
Session session = sf.openSession();
session.beginTransaction();
/*Query q = session.createQuery("from Category c where c.id > :min and c.id < :max");
//q.setParameter("min", 2);
//q.setParameter("max", 8);
q.setInteger("min", 2);
q.setInteger("max", 8);*/
Query q = session.createQuery("from Category c where c.id > :min and c.id < :max")
.setInteger("min", 2).setInteger("max", 8);
List<Category> categories = q.list();
for (Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_06() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c where c.id > ? and c.id < ?");
q.setParameter(0, 2).setParameter(1, 8);
// q.setParameter(1, 8);
List<Category> categories = q.list();
for (Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
//分页
@Test
public void testHQL_07() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c order by c.name desc");
q.setMaxResults(4);
q.setFirstResult(2);
List<Category> categories = q.list();
for (Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_08() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select c.id, c.name from Category c order by c.name desc");
List<Object[]> categories = q.list();
for (Object[] o : categories) {//object的类型就是Category属性的类型
System.out.println(o[0] + "-" + o[1]);
}
session.getTransaction().commit();
session.close();
}
//设定fetch type 为lazy后将不会有第二条sql语句
@Test
public void testHQL_09() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.category.id = 1");
List<Topic> topics = q.list();
for (Topic t : topics) {
System.out.println(t.getTitle());
//System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close();
}
//设定fetch type 为lazy后将不会有第二条sql语句
@Test
public void testHQL_10() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.category.id = 1");
List<Topic> topics = q.list();
for (Topic t : topics) {
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_11() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.topic.category.id = 1");
for (Object o : q.list()) {
Msg m = (Msg) o;
System.out.println(m.getCont());
}
session.getTransaction().commit();
session.close();
}
//了解即可
//VO Value Object
//DTO data transfer object
@Test
public void testHQL_12() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session
.createQuery("select new com.bjsxt.hibernate.MsgInfo(m.id, m.cont, m.topic.title, m.topic.category.name) from Msg");
for (Object o : q.list()) {
MsgInfo m = (MsgInfo) o;
System.out.println(m.getCont());
}
session.getTransaction().commit();
session.close();
}
//动手测试left right join
//使用join后,例如 from Topic t join t.category c,则表示返回的是两种对象了,返回的就是List<Ojbect[]>
//为什么不能直接写Category名,而必须写t.category
//因为有可能存在多个成员变量(同一个类),需要指明用哪一个成员变量的连接条件来做连接
@Test
public void testHQL_13() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select t.title, c.name from Topic t join t.category c "); //不需要用on来表明条件,因为对象间已经有关联关系了
for (Object o : q.list()) {
Object[] m = (Object[]) o;
System.out.println(m[0] + "-" + m[1]);
}
session.getTransaction().commit();
session.close();
}
//学习使用uniqueResult
@Test
public void testHQL_14() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m = :MsgToSearch "); //不重要
Msg m = new Msg();
m.setId(1);
q.setParameter("MsgToSearch", m);
Msg mResult = (Msg) q.uniqueResult();
System.out.println(mResult.getCont());
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_15() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select count(*) from Msg m");
long count = (Long) q.uniqueResult();
System.out.println(count);
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_16() {
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();
}
@Test
public void testHQL_17() {
Session session = sf.openSession();
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.getCont());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_18() {
Session session = sf.openSession();
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.getCont());
}
session.getTransaction().commit();
session.close();
}
//is null 与 is not null
@Test
public void testHQL_19() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.cont is not null");
for (Object o : q.list()) {
Msg m = (Msg) o;
System.out.println(m.getId() + "-" + m.getCont());
}
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();
}
@Entity
@NamedQueries(
{
@NamedQuery(name="topic.selectCertainTopic", query="from Topic t where t.id = :id")
}
)
/*
@NamedNativeQueries(
{
@NamedNativeQuery(name="topic.select2_5Topic", query="select * from topic limit 2, 5")
}
)
*/
public class Topic {
//不重要
@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 = q.list();
for (Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_35() {
//尚未实现JPA命名的NativeSQL
}
@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(//又为Topic里面的category属性添加约束
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();
}
//query by criteria query by example
@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();
}
相关推荐
Hibernate Query Language,简称HQL,是Hibernate框架中用于执行数据库查询的一种面向对象的查询语言。它与传统的SQL语言非常相似,但更简洁,更重要的是,HQL理解面向对象的概念,如继承、多态、关联、聚合和组合。...
Hibernate Query Language(HQL)是Hibernate框架中用于操作对象关系映射(ORM)的一种查询语言。它是面向对象的,设计目的是让开发人员可以使用对象而不是数据库表进行查询,从而简化了与数据库交互的过程。HQL的...
Hibernate Query Language(HQL)是Hibernate官方推荐的查询语言,它是面向对象的,与SQL类似,但更加符合Java编程的思维。HQL使得开发者能够更方便地处理对象关系映射,而无需直接编写SQL语句。在使用HQL时,我们...
首先,让我们了解一下Hibernate中的HQL(Hibernate Query Language),它是Hibernate提供的面向对象的查询语言,类似于SQL,但更贴近于Java。在HQL中,我们可以方便地使用聚合函数进行数据处理。例如,如果你想要...
Hibernate Query Language(简称 HQL)是 Hibernate 提供的一种强大的查询语言,其设计目标是为了提供一种更接近于面向对象编程的查询方式。本文将深入探讨 HQL 的基本概念、语法以及一些高级用法,帮助读者更好地...
Hibernate提供了一种灵活的查询语言——HQL(Hibernate Query Language),以及 Criteria 查询和 Criteria API,它们都可以用来获取数据表中的特定字段。 二、Hibernate配置 在使用Hibernate进行查询前,首先需要...
1. **HQL(Hibernate Query Language)** HQL是Hibernate提供的面向对象的查询语言,类似于SQL,但操作的对象是对象而非表。例如,要获取所有用户,可以使用以下HQL语句: ```java Query query = session....
1. **使用HQL(Hibernate Query Language)** HQL是Hibernate特有的查询语言,类似于SQL,但更面向对象。查询所有数据的HQL语句如下: ```java Session session = sessionFactory.openSession(); Transaction ...
Hibernate Query Language(HQL)是Hibernate框架中用于操作对象关系映射(ORM)的一种查询语言。它类似于SQL,但专为面向对象编程设计,使得开发者能够以类和对象的方式进行数据库查询,极大地简化了数据库操作。在...
在Hibernate中,HQL(Hibernate Query Language)是专为ORM设计的一种面向对象的查询语言,它允许开发者以类和对象的方式进行数据查询,而不是直接使用SQL。本资料主要涵盖了Hibernate HQL查询的基本概念、语法以及...
在Hibernate中,查询数据主要通过Criteria、HQL(Hibernate Query Language)和Query API三种方式。首先,让我们来看看Criteria查询。Criteria API提供了一种类型安全的方式,通过构建查询条件来执行数据库查询。...
8. **查询**: Hibernate支持HQL(Hibernate Query Language)和 Criteria API,它们提供了面向对象的查询方式。另外,还可以使用原生的SQL查询并通过`@NamedNativeQuery`进行配置。 在这个"Spring4Hibernate5MVC...
这里使用了HQL(Hibernate Query Language)来执行查询。例如,在`getProductsMenuSub`方法中,通过以下HQL语句实现了连表查询: ```java String queryString = "select a.productsName from TopProducts as a, ...
本教程将深入探讨Hibernate 3.x版本中的查询方式,包括基本查询、HQL(Hibernate Query Language)以及Criteria API,帮助开发者更高效地处理数据库交互。 1. **基本查询(Criteria API)** Hibernate Criteria ...
2. **HQL(Hibernate Query Language)**:这是Hibernate提供的一种查询语言,类似于SQL,但更加面向对象。通过HQL,我们可以方便地查询数据库中的记录。 3. **Struts标签库**:Struts提供了一系列自定义的JSP标签...
Hibernate 的核心功能之一是提供了一套强大的查询接口和方法,包括基于对象的查询、SQL 查询和 HQL (Hibernate Query Language) 查询等。本文将详细介绍 Hibernate 提供的几种查询方式及其应用场景。 #### 二、重要...
Hibernate Query Language(HQL)是一种面向对象的查询语言,类似于SQL但更专注于对象而不是表格。HQL查询分为HQL查询和HQL命名查询。 A. HQL查询:通过编写HQL语句来执行查询,例如: ```java String hql=...
- **HQL (Hibernate Query Language)** - **Criteria API** - **Native SQL** 本文将重点介绍如何使用HQL和Native SQL来实现多表联合查询。 #### 三、案例背景 假设我们有一个简单的业务场景:需要统计某个时间段...
在`getUser(int offset, int length)`方法中,我们构建HQL(Hibernate Query Language)查询语句,从User表中选取所有记录。使用Session的`createQuery(String hql)`方法创建Query对象,然后设置查询参数,通过`...
首先,我们来看第一种查询方式:HQL(Hibernate Query Language)查询。HQL是Hibernate专有的SQL方言,允许开发者以面向对象的方式编写查询语句。在跟踪HQL源码时,主要关注`Query`接口及其实现类,例如`org....