zoukankan      html  css  js  c++  java
  • hql语法001

    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();
            }
        }
    }
  • 相关阅读:
    shingling算法——提取特征,m个hash函数做指纹计算,针对特征hash后变成m维向量,最后利用union-find算法计算相似性
    普林斯顿算法(1.3)并查集(union-find算法)——本质就是一个数 下面的子树代表了连在一起的点
    Cuckoo hash算法分析——其根本思想和bloom filter一致 增加hash函数来解决碰撞 节省了空间但代价是查找次数增加
    Merkle 树——空间换时间,分而治之的hash表,通过根节点是由它的两个子节点内容的哈希值组成来校验数据完整性,定位篡改的数据位置
    图解Skip List——本质是空间换时间的数据结构,在lucene的倒排列表,bigtable,hbase,cassandra的memtable,redis中sorted set中均用到
    LSM Tree 学习笔记——本质是将随机的写放在内存里形成有序的小memtable,然后定期合并成大的table flush到磁盘
    LSM Tree 学习笔记——MemTable通常用 SkipList 来实现
    Raft 为什么是更易理解的分布式一致性算法——(1)Leader在时,由Leader向Follower同步日志 (2)Leader挂掉了,选一个新Leader,Leader选举算法。
    一个php user class
    CI 模板解析器类
  • 原文地址:https://www.cnblogs.com/syjp/p/11078248.html
Copyright © 2011-2022 走看看