1、

import java.util.List;

import org.hibernate.*;
import org.junit.Test; import cn.jbit.hibernatedemo.dao.HibernateUtil;
import cn.jbit.hibernatedemo.entity.Dept;
import cn.jbit.hibernatedemo.entity.Emp; public class Eg { /**
* 查询工资高于平均工资的员工。
*/
@Test
public void egEmp() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Emp> list = session.createQuery(
"from Emp e where e.salary>(select avg(salary) from Emp)")
.list();
for (Emp emp : list) {
System.out.println(emp.getEmpName() + "," + emp.getSalary());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
HibernateUtil.closeSession();
}
} /**
* 查询所有员工工资都小于5000的部门。
*/
@Test
public void eg5() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Dept> list = session
.createQuery(
"from Dept d where 5000>all(select e.salary from d.emps e) and d.emps.size>0")
.list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
if (session != null)
session.close();
}
} /**
* 查询至少有一位员工工资低于5000的部门。
*/
@Test
public void eg6() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Dept> list = session
.createQuery(
"from Dept d where 5000>any(select e.salary from d.emps e)")
.list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
if (session != null)
session.close();
}
} /**
* ,查询员工工资正好是5000元的部门
*/
@Test
public void eg7() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Dept> list = session
.createQuery(
"from Dept d where 5000=any(select e.salary from d.emps e)")
.list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
if (session != null)
session.close();
}
} /**
* 查询员工工资正好是5000元的部门
*/
@Test
public void eg7_1() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Dept> list = session
.createQuery(
"from Dept d where 5000=some(select e.salary from d.emps e)")
.list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
if (session != null)
session.close();
}
} /**
* 查询员工工资正好是5000元的部门
*/
@Test
public void eg7_2() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Dept> list = session
.createQuery(
"from Dept d where 5000 in (select e.salary from d.emps e)")
.list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
HibernateUtil.closeSession();
}
} /**
* 查询至少有一位员工的部门
*/
@Test
public void eg8() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Dept> list = session.createQuery(
"from Dept d where exists (from d.emps)").list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
HibernateUtil.closeSession();
}
} /**
* 查询指定员工所在部门
*/
@Test
public void eg9() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
Emp emp = new Emp();
emp.setEmpNo(1);
List<Dept> list = session
.createQuery("from Dept d where ? in elements (d.emps)")
.setParameter(0, emp).list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
HibernateUtil.closeSession();
}
} /**
* 查询指定员工所在部门
*/
@Test
public void eg9_1() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
Emp emp = new Emp();
emp.setEmpNo(1);
List<Dept> list = session
.createQuery("from Dept d where ? in (from d.emps)")
.setParameter(0, emp).list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
HibernateUtil.closeSession();
}
} /**
* 查询员工个数大于5的部门
*/
@Test
public void eg10() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Dept> list = session.createQuery(
"from Dept d where d.emps.size>5").list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
HibernateUtil.closeSession();
}
} /**
* 查询员工个数大于5的部门
*/
@Test
public void eg10_1() {
Session session = null;
try {
// 获取session
session = HibernateUtil.currentSession();
List<Dept> list = session.createQuery(
"from Dept d where size(d.emps)>5").list();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭session
HibernateUtil.closeSession();
}
}
}

hql语法001的更多相关文章

  1. jpa 联表查询 返回自定义对象 hql语法 原生sql 语法 1.11.9版本

    -----业务场景中经常涉及到联查,jpa的hql语法提供了内连接的查询方式(不支持复杂hql,比如left join ,right join).  上代码了 1.我们要联查房屋和房屋用户中间表,通过 ...

  2. 查询总结、HQL语法、QBC(QueryByCriteria)深入学习

    1.查询总结 在之前的批量查询练习的时候练习基本五种查询方法的使用: 1.OID查询---根据对象ID进行查询 2.对象属性导航查询: obj.getXXX 3.HQL查询:Query对象查询 4.Q ...

  3. Hibernate 框架 -HQL 语法

    HQL ( Hibernate Query Language ) 查询语言是面向对象的查询语言,也是在 Hibernate 中最常见的.其语法和 SQL 语法有一些相似,功能十分强大,几乎支持除特殊 ...

  4. 一脸懵逼学习Hive的使用以及常用语法(Hive语法即Hql语法)

    Hive官网(HQL)语法手册(英文版):https://cwiki.apache.org/confluence/display/Hive/LanguageManual Hive的数据存储 1.Hiv ...

  5. [转]hql 语法与详细解释

    HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hib ...

  6. HQL语法

    HQL:Hibernate Query Language HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征. $下面介绍HQL语句的语法 1.from子句 from Person 表明从P ...

  7. hql 语法详解

    HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hib ...

  8. hql 语法与详细解释<转>

    HQL查询 HQL查询: Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性 ...

  9. 3 hql语法及自定义函数(含array、map讲解) + hive的java api

    本博文的主要内容如下: .hive的详细官方手册    .hive支持的数据类型   .Hive Shell .Hive工程所需依赖的jar包  .hive自定义函数 .分桶4   .附PPT hiv ...

随机推荐

  1. WIN7开机自动登录设置

    WIN7开机自动登录设置 1    执行rundll32 netplwiz.dll,UsersRunDll 开始菜单中找到运行并单击运行:如下图1所示                         ...

  2. 小程序不支持wx.request同步请求解决方法

    小程序为了用户体验,所有的request均为异步请求,不会阻塞程序运行 百牛信息技术bainiu.ltd整理发布于博客园 所以当你需要同步请求,锁死操作时,最好将所有的逻辑写在success:func ...

  3. Bootstrap-CSS:图片

    ylbtech-Bootstrap-CSS:图片 1.返回顶部 1. Bootstrap 图片 在本章中,我们将学习 Bootstrap 对图片的支持.Bootstrap 提供了三个可对图片应用简单样 ...

  4. webpack项目调试以及独立打包配置文件

    webpack项目调试 -sourcemap webpack配置提供了devtool这个选项,如果设置为 ‘#source-map’,则可以生成.map文件,在chrome浏览器中调试的时候可以显示源 ...

  5. B. Mishka and trip

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  6. git cherry-pick简介(转载)

    转自:http://hubingforever.blog.163.com/blog/static/1710405792012587115533/ 本文编辑整理自: http://sg552.iteye ...

  7. ObjectARX学习笔记

    http://blog.csdn.net/jangdong/article/category/1642265/3 http://blog.csdn.net/u011331383/article/cat ...

  8. hdoj1575【矩阵快速幂】

    题意:非常清晰! 直接搞吧. #include <iostream> #include <stdio.h> #include <string.h> #include ...

  9. 浅谈字符串哈希 By cellur925

    前言 蒟蒻最近在复习字符串算法...但正如之前所说,我OI太菜被关起来了,本蒟蒻只能从最简单的哈希入手了TAT.而别的dalao都在学习AC自动机/后缀数组等高到不知哪里去的算法qwq. 基本思想 映 ...

  10. hql语法及自定义函数(含array、map讲解) + hive的java api

    本博文的主要内容如下: .hive的详细官方手册    .hive支持的数据类型   .Hive Shell .Hive工程所需依赖的jar包  .hive自定义函数 .分桶4   .附PPT hiv ...