上回写到一个一个最基本的HQL 查询语句写出来都没有什么自信,这一课时就补上HQL 相关的知识。

这种东西笔者最喜欢的官方的原版说明文档了。

http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch11.html

53.1 大小写敏感度

HQL 中的Java 类和属性是大小写敏感的,其他都是大小写不敏感的。一般来说,我们约定试用全小写的形式。

53.2 CURD 语法

基本的select update delete insert 语法。

53.2.1 Select 语句

范式如下:

select_statement :: =
[select_clause]
from_clause
[where_clause]
[groupby_clause]
[having_clause]
[orderby_clause]

最简单的形式可以这样:

from com.acme.Cat

JPQL 中的Select 语句和HQL 中相同,唯一的区别就是JPQL 中不能省略 [select_clause]

所以如果以后有移植JPA 需要的话,建议都把 [select_clause] 带上。当然在风格上来说,我们也建议都带上 [select_clause].

53.2.2 Update 语句

范式如下:

update_statement ::= update_clause [where_clause]

update_clause ::= UPDATE entity_name [[AS] identification_variable]
        SET update_item {, update_item}*

update_item ::= [identification_variable.]{state_field | single_valued_object_field}
        = new_value

new_value ::= scalar_expression |
                simple_entity_expression |
                NULL

以下是三种形式实现的示例

String hqlUpdate =
"update Customer c " +
"set c.name = :newName " +
"where c.name = :oldName";
int updatedEntities = session.createQuery( hqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();

String jpqlUpdate =
"update Customer c " +
"set c.name = :newName " +
"where c.name = :oldName";
int updatedEntities = entityManager.createQuery( jpqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();

String hqlVersionedUpdate =
"update versioned Customer c " +
"set c.name = :newName " +
"where c.name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();

关于Version, 暂时不深入。HQL 直接居然可以CURD,对比EF 和LINQ, 我震惊了。

53.2.3 Delete 语句

范式如下:

delete_statement ::= delete_clause [where_clause]

delete_clause ::= DELETE FROM entity_name [[AS] identification_variable]

53.2.4 Insert 语句

这个只有HQL 的实现,JPQL 里面没有。

范式如下:

insert_statement ::= insert_clause select_statement

insert_clause ::= INSERT INTO entity_name (attribute_list)

attribute_list ::= state_field[, state_field ]*

53.3 From 子句

From 用来界定Scope, 同时也可以在这个Scope 中使用别名。

53.3.1 标记变量

标记变量说白了就是别名。

使用别名是可选的,当然这个是一个很好的风格。

53.3.2 Root entity 的引用

select c from com.acme.Cat c

当然我们一般不写全类名,我们这么用:

select c from Cat c
select distinct c1
from Customer c1, Customer c2
where c1.address.state = c2.address.state
  and c2.name = 'Acme'

没啥好说的,别名大家都会用。

剩余部分留待以后

Note:

Plan List:

1 MySql

2 JVM

3 HQL 剩余部分。

4 博客增加一种H4的style

最新文章

  1. CSUOJ_1000
  2. meta 标签 详细说明
  3. 【HOW】如何配置SharePoint传入/传出电子邮件设置
  4. .this语句指的是什么
  5. 分片传输——send和recv函数
  6. “声控”APP
  7. android性能优化优秀文章
  8. H5播放器内置播放视频(兼容绝大多数安卓和ios)
  9. vue项目安装vux
  10. mysql 的crud操作(增删改查)
  11. Crashing Robots(水题,模拟)
  12. Maven使用lib下的包
  13. php mysql 丢失更新
  14. 跨域请求:JSONP
  15. Python 入门基础3 --流程控制
  16. 缓冲区溢出基础实践(二)——ROP 与 hijack GOT
  17. usb端点(endpoint)知识详解
  18. chkconfig --add失败的处理方法
  19. 1.HTML练习(二)
  20. awk 正则表达式、正则运算符详细介绍

热门文章

  1. nuget pack
  2. C# ManualResetEvent 的方法介绍
  3. struts+spring action应配置为scope="prototype"
  4. struts2拦截器interceptor的三种配置方法
  5. object-c 的ARC 问答/介绍
  6. javascript单体模式
  7. 保护隐私:清除cookie、禁用cookie确保安全【分享给身边的朋友吧】
  8. 教你如何---构建良好的windows程序(初学者必看)
  9. TASKKILL命令使用大全
  10. Unity 3D 粒子系统的一点经验