hibernate查询语句--HQL

简介:

1.from
 1.1单表查询
    from eg.cat as cat.其中,cat只是一个别名,为了用其他子语句的时候书写简单
 1.2多表查询
    from eg.Cat,eg.Dog
    from eg.Cat as cat,eg.Dog as dog

2.join相关
  (inner) join
  left (outer) join
  right (outer) join
  full join
  HQL同样对SQL中的这些特性支持
  下面插播一个小话题,关于上边的那些特性,我一直都没怎么用,今天既然说到这里,就想把上边的几个特性的用法说一下,也算对自己的一个补充:
  假设有两个表:部门、员工,下面列举一些数据:
     员工(Employee):
      ID   Name   DepNo
      001  Jplateau   01
      002  Jony     01
      003  Camel    02
     部门(Department):
      ID   Name
      01   研发部
      02   营销部
  在Hibernate中我们操纵的都是对象,所以我们操纵的是部门类和员工类
  1).(inner) join
   select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 from Employee as employee join Department as department on employee.DepNo=department.ID (注意到条件语句我用on 没有用where)
   那么执行结果是什么呢?
     id1    name1   id2   name2
    ++++++++++++++++++++++++++++++++++++++
     001   Jplateau   01   研发部
     002   Jony     01   研发部

  2).left (outer) join
   select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 from Employee as employee left join Department as department on employee.DepNo=department.ID
   那么执行结果又该是什么呢?
     id1    name1   id2   name2
    ++++++++++++++++++++++++++++++++++++++
     001   Jplateau   01   研发部
     002   Jony     01   研发部
     003   Camel    null   null
   就是说此时我要已第一个表的记录多少为准,第二个表中没有相应纪录的时候填充null。
  3). right (outer) join
   select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 from Employee as employee right join Department as department on employee.DepNo=department.ID
   那么执行结果又该是什么呢?
     id1    name1   id2   name2
    ++++++++++++++++++++++++++++++++++++++
     001   Jplateau   01   研发部
     002   Jony     01   研发部
     nul    null     02   营销部
   就是说此时我要已第二个表的记录多少为准,第一个表中没有相应纪录的时候填充null。

3.大小写敏感

4.select语句
  就是要确定你要从查询中返回哪些对象或者哪些对象的属性。写几个例子吧:
   select employee form Employee as employee
   select employee form Employee as employee where employee.Name like 'J%'
   select employee.Name form Employee as employee where employee.Name like 'J%'
   select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 from Employee as employee right join Department as department on employee.DepNo=department.ID
   select elements(employee.Name) from Employee as employee

   (不明白elements到底是做什么用的?望给于说明)
  等等

5.数学函数
  JDO目前好像还不支持此类特性。
  avg(...), sum(...), min(...), max(...)
  count(*)
  count(...), count(distinct ...), count(all...)
  其用法和SQL基本相同:
   select distinct employee.name from Employee as employee
   select count(distinct employee.name),count(employee) from Employee as employee


6.polymorphism (暂时不知道如何解释?)
  from com.test.Animal as animal
  不光得到所有Animal得实例,而且可以得到所有Animal的子类(如果我们定义了一个子类Cat)
  一个比较极端的例子
  from java.lang.Object as o
  可以得到所有持久类的实例

7.where语句
  定义查询语句的条件,举几个例子吧:
  from Employee as employee where employee.Name='Jplateau'
  from Employee as employee where employee.Name like 'J%'
  from Employee as employee where employee.Name like '%u'
  在where语句中“=”不光可以比较对象的属性,也可以比较对象,如:
  select animal from com.test.Animal as animal where animal.name=dog

8.表达式
  在SQL语句中大部分的表达式在HQL中都可以使用:
  mathematical operators:    +, -, *, /
  binary comparison operators:  =, >=, <=, <>, !=, like
  logical operations:       and, or, not
  string concatenation:      ||
  SQL scalar functions:      like upper() and lower()
  Parentheses ( ) indicate grouping in, between, is null
  JDBC IN parameters ?
  named parameters        :name, :start_date, :x1 (这种应该是另一种"?"的变通解决方法)
  SQL literals 'foo', 69, '1970-01-01 10:00:01.0'
  Java public static final constants eg.Color.TABBY
  其他不必解释了,在这里我只想对查询中的参数问题说明一下:
  大家知道在SQL中进行传递参数进行查询的时候,我们通常用PreparedStatement,在语句中写一大堆的“?”,
  在hql中也可以用这种方法,如:
  List mates = sess.find(
     "select employee.name from Employee as employee " +
     "where employee.Name=? ",
     name,
     Hibernate.STRING
  );
  说明:上面利用Session里的find方法,在hibernate的api Session中重载了很多find方法,它可以满足你多种形式的查询。上边是一个参数的情形,这种情况下紧接着引入参数和定义参数的类型,当为多个参数,调用另一个find方法,它的后两个参数都是数组的形式。
  还有另外一种方法来解决上边的问题,JDO也有这样的方法,不过和hibernate的表现形式上有差别,但他们两个骨子里却是一样的,如:
  Query q = sess.createQuery("select employee.name from Employee as employee where employee.Name=:name");
  q.setString("name", "Jplateau");
  //当有多个参数的时候在此逐一定义
  Iterator employees = q.iterate();

9.order 语句
  和sql语句没什么差别,如:
  select employee.name from Employee as employee where employee.Name like 'J%' order by employee.ID desc (或者asc)

10.group by 语句
  同样和sql语句没什么差别,如:
  select employee.name,employee.DepNo from Employee as employee group by employee.DepNo
  select foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.Foo foo group by foo.id
  {Note: You may use the elements and indices constructs inside a select clause, even on databases with no subselects.}
谁帮我解释一下上边两句,谢过!

11.子查询
  hibernate同样支持子查询,写几个例子:
  from eg.Cat as fatcat where fatcat.weight > ( select avg(cat.weight) from eg.DomesticCat cat )

目录
相关文章
|
2月前
|
SQL 数据库 Java
HQL vs SQL:谁将统治数据库查询的未来?揭秘Hibernate的神秘力量!
【8月更文挑战第31天】Hibernate查询语言(HQL)是一种面向对象的查询语言,它模仿了SQL的语法,但操作对象为持久化类及其属性,而非数据库表和列。HQL具有类型安全、易于维护等优点,支持面向对象的高级特性,内置大量函数,可灵活处理查询结果。下面通过示例对比HQL与SQL,展示HQL在实际应用中的优势。例如,HQL查询“从员工表中筛选年龄大于30岁的员工”只需简单地表示为 `FROM Employee e WHERE e.age &gt; 30`,而在SQL中则需明确指定表名和列名。此外,HQL在处理关联查询时也更为直观易懂。然而,对于某些复杂的数据库操作,SQL仍有其独特优势。
39 0
|
2月前
|
API Java 数据库连接
从平凡到卓越:Hibernate Criteria API 让你的数据库查询瞬间高大上,彻底告别复杂SQL!
【8月更文挑战第31天】构建复杂查询是数据库应用开发中的常见需求。Hibernate 的 Criteria API 以其强大和灵活的特点,允许开发者以面向对象的方式构建查询逻辑,同时具备 SQL 的表达力。本文将介绍 Criteria API 的基本用法并通过示例展示其实际应用。此 API 通过 API 构建查询条件而非直接编写查询语句,提高了代码的可读性和安全性。无论是简单的条件过滤还是复杂的分页和连接查询,Criteria API 均能胜任,有助于提升开发效率和应用的健壮性。
62 0
|
2月前
|
SQL Java 数据库连接
|
2月前
|
缓存 Java 数据库连接
什么是 Hibernate 查询语言或 HQL?
【8月更文挑战第21天】
68 0
|
2月前
|
SQL Java 数据库连接
在 Hibernate 中何时使用条件查询?
【8月更文挑战第21天】
28 0
|
2月前
|
缓存 Java 数据库连接
Hibernate 中的查询缓存是什么?
【8月更文挑战第21天】
23 0
|
2月前
|
SQL 安全 Java
|
4月前
|
SQL Java 数据库连接
技术经验分享:Hibernate之HQL
技术经验分享:Hibernate之HQL
17 0
|
5月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——( EmpCondition)
Hibernate中使用Criteria查询及注解——( EmpCondition)
|
5月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(DeptTest.java)
Hibernate中使用Criteria查询及注解——(DeptTest.java)