Hibernate ORM框架——Hibernate查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate ORM框架——Hibernate查询相关的知识,希望对你有一定的参考价值。

 

mysql数据库--------------------------------------------------

use demo;

select * from CLASSINFO;
select * from persons;

delete from CLASSINFO;
delete from persons;

insert into CLASSINFO values(c111,banji1);
insert into CLASSINFO values(c222,banji2);


insert into persons values(p1,p111,c111)
insert into persons values(p2,p222,c111)
insert into persons values(p3,p333,c222)
insert into persons values(p4,p444,c222)
insert into persons values(p5,p555,null)

select pid,name from persons order by pid desc;--降序
select pid,name from persons order by pid asc;--升序


select COUNT(*),name from persons group by name

select (select pid,name from persons),p.rci from persons p left join CLASSINFO c on p.rci=c.cid group by p.rci;



//查询------------------------------------------------------
1.hibernate整体上的查询分为3类
    (1)HQL(Hibernate Query Languange)
        面相对象的查询语言,所以跨数据库
    (2)Native SQL(本地sql,原生的某个特定的数据的sql)
    (3)critiria(规则)
    (4)命名查询(Named Query)


2.从行数来区分,hibernate提供了
    (1)list方法:多行
    (2)uniqueResult:单行
        的查询方式


3.Hql语句的大小写问题
    (1)hql里面的实体,实体的属性是区分大小写的    eg:from Person(实体名);
    (2) 其它的不区分    eg:from Person(实体名) where name(属性名)="";即where后面的
     
 
4. 关于查询需要涉及的知识点
    (1) 排序,
     (2)过滤,
     (3)分组,
     (4)聚合函数,
     (5)列数的控制(select name,age)
 

  注意点: 
     (1)查询语句中加别名的规范:
         Person [as] p
        选取所有列,直接写p
     
    (2)不能直接在查询列里面写上做关联的字段
        因为它一般牵涉到多表的查询处理
        


总结上午学习知识点:
1.查询所有
2.过滤条件
    (1)写死
    (2)?
    (3):
3.排序(升序,降序)
4.聚合函数
5.分组
6.list方法和uniqueResult方法

-------子查询,链接查询-----

------native sql-------

---规则(criteria)

项目一:

(1)配置文件不变

(2)HibernateUtil不变

(3.1)班级实体

   private String cid ;
    private String name;
    //一对多
    private Set<Person> persons;

(3.2)学生实体

  private String pid ;//UUID
    private String name;
    private ClassInfo ci;
  //多对一

(4)两个实体映射文件和上一篇一样(双向多对一,一对多)

(5)Main测试

package com.nf;



import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import shuang.m2oo2m.ClassInfo;
import shuang.m2oo2m.Person;
import util.HibernateUtil;

public class Main {

    public static void main(String[] args) {
        /*在List中使用,泛型object[]与泛型实体类的区别
         * 由于实体类重写了toString方法,
         * 所以select p from Person p中的p与实体的字段与属性名一样
         * 所以可以直接使用泛型实体类遍历
         * 
         * 而如果查询语句的*,是p.name,p.pid之类的,只有两列,与实体中的有三个属性不匹配,
         * 所以需要使用泛型object[]遍历
         * */
        
        
        
        //String hql="select p from Person p";
        //queryAll();

        //过滤条件:?参数
        //wenhaoParam();
        
        //过滤条件:命名参数
        //NameParam();
        
        //排序(升序,降序)
        //paixu();
        
        //聚合函数、分组
        //groupBy();
        
        
        groupByCid();

    }
    
    public static void queryAll() {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        
        /*String hql="select p from Person p";*/
        String hql="from Person";
        
        Query q = s.createQuery(hql);
        List<Person> list = q.list();
        System.out.println(list.size());//5
        for(Person data : list){
            //System.out.println(data.getName());
            /*结果
             * 5
            p111
            p222
            p333
            p444
            p555    
             * */
            
            //System.out.println(data);
            /*结果
             * 5
            Person [pid=p1, name=p111]
            Person [pid=p2, name=p222]
            Person [pid=p3, name=p333]
            Person [pid=p4, name=p444]
            Person [pid=p5, name=p555]
             * */
            
            
            System.out.println(data+"_____and_____"+data.getCi());
            /*结果:
             * 
Person [pid=p1, name=p111]_____and_____ClassInfo [cid=c111, name=banji1]
Person [pid=p2, name=p222]_____and_____ClassInfo [cid=c111, name=banji1]

Person [pid=p3, name=p333]_____and_____ClassInfo [cid=c222, name=banji2]
Person [pid=p4, name=p444]_____and_____ClassInfo [cid=c222, name=banji2]
Person [pid=p5, name=p555]_____and_____null
             * */
        }
        
        tx.commit();
        s.close();
        sf.close();
        
    }
    
    
    public static void wenhaoParam() {
        
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        //String hql = "select new list(p.pid,p.name) from Person as p ";
        //转换格式list(p.pid,p.name)的话,则需要用object遍历,否则直接用Person实体遍历
        String hql = "select p.name,p.pid from Person as p where rci =?";
        Query q = s.createQuery(hql);
        ClassInfo cls = new ClassInfo();
        cls.setCid("c222");
        q.setParameter(0, cls);//?的设值
        
        List<Object[]> list = q.list();
        
        //System.out.println(list.get(0));
        //[Ljava.lang.Object;@25c5e994
        
        for (Object[] objects : list) {
            System.out.println(objects[0]);
            System.out.println(objects[1]);
        }
        
        /*结果:
         * p111
            p1*/
        tx.commit();
        s.close();
        sf.close();
    }
    
    
    public static void NameParam() {
        
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();

        //查询实体的所有记录
        String hql = "select p.name,p.pid from Person as p where name =:myname";
        
        Query q = s.createQuery(hql);
        
        q.setParameter("myname", "p111");//:命名参数
        
        List<Object[]> list = q.list();
        for(Object[] data : list){
            System.out.println(data[0]);
            System.out.println(data[1]);
        }
        /*结果:
         * p111
            p1*/        
        tx.commit();
        s.close();
        sf.close();
    }


    public static void paixu() {
        
        SessionFactory sf= HibernateUtil.getSessionFactory();
        
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        
        //String hql="select pid,name from Person order by pid asc";//升序
        String hql="select p.pid,p.name from Person p order by pid desc";//降序
        
        Query q = s.createQuery(hql);
        List<Object[]> list = q.list();
        for (Object[] object : list) {
            System.out.println(object[0]);
            System.out.println(object[1]);
        }
        
        tx.commit();
        s.close();
    }
    
    
    public static void groupBy() {
        
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        
        //String hql = "select COUNT(name),name from Person group by name ";
        String hql = "select COUNT(*),name from Person group by name ";
        
        Query q = s.createQuery(hql);
        List<Object[]> list = q.list();
        for (Object[] objects : list) {
            System.out.println(objects[0]);
            System.out.println(objects[1]);
        }
        
        tx.commit();
        s.close();
        
    }

}

 

 

 

 

 

项目二:(集合相关)

返回类型
a)返回实体集合List<Person>
b)返回Object[]集合List<Object[]>
c)返回Map集合List<Map>  new map
d)返回List的集合List<List> new list
e)返回自定义类型集合List<定制类型>  new PersonInfo(renid,renmingzi,banji)
    PersonInfo

-------子查询,链接查询-----

------native sql-------

---规则(criteria)

 

Main测试

package com.nf;



import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import shuang.m2oo2m.Person;
import shuang.m2oo2m.PersonInfo;
import util.HibernateUtil;

public class Main {

    public static void main(String[] args) {
        //queryAll();
        
        //List集合中放着map集合
        //returnListMap();
        
        //List集合里放-自定义类型
        //returnCustomClass();
        
        //in范围
        //inQuery();
        inQuery2();
    }

    private static void template() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
    
        tx.commit();
        s.close();
        sf.close();
    }

    private static void queryAll() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        //String hql = "from Person";
        String hql = "select p from Person p";
        Query q = s.createQuery(hql);
        List<Person> list = q.list();
        for(Person p: list){
            System.out.println(p);
        }
        
        tx.commit();
        s.close();
        sf.close();
    }
    
    
    private static void returnListMap() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
    
        /*String hql = "select new map(p.pid,p.name) from Person p";
        Query q = s.createQuery(hql);
        List<Map> list = q.list();
        for(Map p: list){
            System.out.println(p.get("0"));//默认pid的别名为0
            System.out.println(p.get("1"));//默认name的别名为1
        }*/
        
        String hql = "select new map(p.pid as bianhao,p.name as mingzi) from Person p";
        Query q = s.createQuery(hql);
        List<Map> list = q.list();
        for(Map p: list){
            System.out.println(p.get("bianhao"));
            System.out.println(p.get("mingzi"));
        }
        
        tx.commit();
        s.close();
        sf.close();
    }
    
    
    private static void returnCustomClass() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
    
        String hql = "select new shuang.m2oo2m.PersonInfo(p.pid,p.name) from Person p";
        Query q = s.createQuery(hql);
        
        List<PersonInfo> list = q.list();
        for(PersonInfo p: list){
            System.out.println(p);        
        }
        tx.commit();
        s.close();
        sf.close();
    }
    
    
    private static void inQuery() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
    
        String hql = "select p from Person p where p.name in (‘p111‘,‘p333‘)";
        Query q = s.createQuery(hql);
        List<Person> list = q.list();
        for(Person p: list){
            System.out.println(p);        
        }
        tx.commit();
        s.close();
        sf.close();
    }
    
    
    private static void inQuery2() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
    
        String hql = "select p from Person p where p.name in :names";
        Query q = s.createQuery(hql);
        List<String> allnames = new ArrayList<>();
        allnames.add("p111");
        allnames.add("p444");
        q.setParameter("names", allnames);
        List<Person> list = q.list();
        for(Person p: list){
            System.out.println(p);        
        }
        tx.commit();
        s.close();
        sf.close();
    }
}

 

//List集合里放-自定义类型:相关的类

PersonInfo类

package shuang.m2oo2m;

public class PersonInfo {

    private String xx;
    private String yy;
    
    public PersonInfo(){
        
    }
    
    public PersonInfo(String aa,String bb){
        this.xx = aa;
        this.yy = bb;
    }
    public String getXx() {
        return xx;
    }
    public void setXx(String xx) {
        this.xx = xx;
    }
    public String getYy() {
        return yy;
    }
    public void setYy(String yy) {
        this.yy = yy;
    }

    @Override
    public String toString() {
        return "PersonInfo [xx=" + xx + ", yy=" + yy + "]";
    }
    
    
    
}

 

 

 

 

 

/*以上个人整理笔记,如果有误或者有不懂的地方,欢迎评论与指出*/

以上是关于Hibernate ORM框架——Hibernate查询的主要内容,如果未能解决你的问题,请参考以下文章

hibernate进阶hibernate基本映射

Mybaitis和Hibernate的区别

Hiberante知识点梳理

Hibernate框架配置

实际开发mybatis都是使用自动生成的sql么

Hibernate二级缓存 --Hibernate框架