hql与sql的区别

hql与sql的区别
   sql 面向数据库表查询
   hql 面向对象查询
   hql : from 后面跟的 类名+类对象 where 后 用 对象的属性做条件查询
   sql: from 后面跟的是表名             where 后 用表中字段做条件
    在Hibernate中使用查询时,一般使用Hql查询语句。
    HQL(Hibernate Query Language),即Hibernate的查询语言跟SQL非常相像。
    不过HQL与SQL的最根本的区别,就是它是面向对象的。




  1.(HQL) 特性:大小写敏感;from语句;
                 join相干 (inner) join left   (outer) join
                           right (outer) join   full join 


  2.join语句:
    
员工(Employee):         部门(Department):
ID Name DepNo              ID Name
001 Jplateau 01            01 研发部
002 Jony 01                02 营销部
003 Camel 02


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 研发部
null null 02 营销部
{就是说此时我要已第二个表的记录多少为准,第一个表中没有相应纪录的时候填充null}


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 ||


还有另外一种方法来解决上边的问题,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


1.1简单查询
List list = session.createQuery("from User as user order by user.loginName").list();
1.2带单个参数的查询

List list = session.find("from User as user where user.loginName=?",loginName,Hibernate.STRING);

最近在SSH项目中,需要编写很多HQL语句,正好整理学习下。








你可能感兴趣的:(工作总结,hql与sql的区别,HQL语句,SQL语句,hibernate,HQL,SQL查询)