Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Hibernate HQL 语句

Hibernate HQL 语句

作者头像
Hongten
发布于 2018-09-18 02:22:00
发布于 2018-09-18 02:22:00
6260
举报
文章被收录于专栏:HongtenHongten

/**  *  */ package com.b510.example;

import java.util.Iterator; import java.util.List; import java.util.Map;

import org.hibernate.Query; import org.hibernate.Session;

/**  *  * @author XHW  *  * @date 2011-6-18  *  */ public class HibernateTest {

 /**   * @param args   */  public static void main(String[] args) {   HibernateTest test=new HibernateTest();   test.list();   test.oneResult();   test.iterate();   test.iterateHuancun();   test.jiChenChaXun();   test.touYinChaXun();   test.touYinChaXunMap();   test.touYinChaXunObject();   test.checkDistinct();   test.where(); }  public void list(){   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("from User");   query.setMaxResults(2);//返回2条记录   query.setFirstResult(2);//从第2条记录开始   List<User> list=query.list();   session.getTransaction().commit();   for(User user:list){    System.out.println(user.getId()+", "+user.getUsername()+",  "+user.getPassword());   }  }

 public void oneResult(){   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("from User");   query.setMaxResults(1);//返回1条记录   query.setFirstResult(2);//从第2条记录开始   User user=(User)query.uniqueResult();   System.out.println(user.getId()+", "+user.getUsername());   session.getTransaction().commit();  }  public void iterate(){//此方法的SQL语句格式为  n+1   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("from User");   Iterator it=query.iterate();   while(it.hasNext()){    User user=(User)it.next();    System.out.println(user.getId()+", "+user.getUsername()+",  "+user.getPassword());   }   session.getTransaction().commit();  }  public void iterateHuancun(){   // 充分利用Hibernate中的缓存数据 如果Hibernate缓存中不存在数据,则使用list方法执行效率更高   //List<User> list=query.list();   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("from User");   Iterator it=query.iterate();   while(it.hasNext()){    User user=(User)it.next();    System.out.println(user.getId()+", "+user.getUsername()+",  "+user.getPassword());   }   System.out.println("-----------------------------------------------");   it=query.iterate();   while(it.hasNext()){    User user=(User)it.next();    System.out.println(user.getId()+", "+user.getUsername()+",  "+user.getPassword());   }     session.getTransaction().commit();  }

 public void jiChenChaXun(){   /**    *  HQL中持久化类的继承查询    hibernate.cfg.xml 中的配置文件    <mapping resource="com/b510/example/User.hbm.xml" />    <mapping resource="com/b510/example/Profile.hbm.xml" />    */   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("from java.lang.Object");   List<Object> list=query.list();   for(Object o:list){    System.out.println(o.getClass().getName());   }     session.getTransaction().commit();  }  public void touYinChaXun(){//投影查询  返回的是一个Object数组   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("select id,username,password from User");     List<Object[]> list=query.list();    session.getTransaction().commit();

   for(Object[] o:list){     System.out.println(o[0]+",  "+o[1]+",  "+o[2]);    }  }  public void touYinChaXunObject(){   //投影查询 返回的是一个对象   /**    * 在User.java持久化类中添加构造方法才能执行,否则就会报错    * public User(Integer id,String username,String password){    * this.id=id;    * this.username=username;    * this.password=password;    * }    */   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("select new User(id,username,password) from User");     List<User> list=query.list();    session.getTransaction().commit();

   for(User user:list){     System.out.println(user.getId()+",  "+user.getUsername()+",  "+user.getPassword());    }  }

 public void touYinChaXunMap(){   //使用Map查询   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("select new Map(u.id as id,u.username as name) from User as u");   //可以把as省略   //Query query=session.createQuery("select new Map(u.id as id,u.username as name) from User u");     List<Map> list=query.list();

  for(Map map:list){     System.out.println(map.get("id")+",   "+map.get("name"));    }    session.getTransaction().commit();  }  public void checkDistinct(){   //使用distinct查询   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("select distinct username from User");   List<String> list=query.list();

  for(String name:list){     System.out.println(name);    }    session.getTransaction().commit();  }  public void where(){   //使用where查询   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Query query=session.createQuery("from User where id not between 200 and 2000");   List<User> list=query.list();

  for(User user:list){     System.out.println(user.getId()+user.getUsername());    }   //投影查询 中使用where子句   query=session.createQuery("select username from User where id=2");   List<String> listname=query.list();

  for(String name:listname){     System.out.println(name);    }   //in查询   query=session.createQuery("from User where username in ('Hongten','Hanyuan','dfgd')");   List<User> listin=query.list();

  for(User user:listin){     System.out.println(user.getId()+user.getUsername());    }   //like查询   query=session.createQuery("from User where username not like 'Hon%'");   List<User> listlike=query.list();

  for(User user:listlike){     System.out.println(user.getId()+user.getUsername());    }   //null查询   query=session.createQuery("from User where password is null");   List<User> listnull=query.list();

  for(User user:listnull){     System.out.println(user.getId()+user.getUsername());    }   //and查询   query=session.createQuery("from User where password is not null and id<5");   List<User> listand=query.list();   for(User user:listand){     System.out.println(user.getId()+user.getUsername()+user.getPassword());    }   //order by   query=session.createQuery("from User order by username,id desc");   List<User> listorderby=query.list();

  for(User user:listorderby){     System.out.println(user.getId()+user.getUsername());    }   //使用"?"号 作为参数占位符,一条HQL语句中可以使用多个?   //query.setInteger(0,2)   //query.setString(0,"Hongten")   query=session.createQuery("select username from User where username=?");   query.setString(0,"Hongten");   List<String> listwenhao=query.list();   for(String name:listwenhao){     System.out.println(name);    }    session.getTransaction().commit();  } }

结果:

Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_ limit ?,         ? 3, Hanyuan,  5645645 4, dfgd,  5645645 Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_ limit ?,         ? 3, Hanyuan Hibernate:     select         user0_.id as col_0_0_     from         users.user user0_ Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 1, Hongten,  123 Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 2, Hanyuan,  5645645 Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 3, Hanyuan,  5645645 Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 4, dfgd,  5645645 Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 5, dfgd,  5645645 Hibernate:     select         user0_.id as col_0_0_     from         users.user user0_ Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 1, Hongten,  123 Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 2, Hanyuan,  5645645 Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 3, Hanyuan,  5645645 Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 4, dfgd,  5645645 Hibernate:     select         user0_.id as id0_0_,         user0_.username as username0_0_,         user0_.password as password0_0_     from         users.user user0_     where         user0_.id=? 5, dfgd,  5645645 ----------------------------------------------- Hibernate:     select         user0_.id as col_0_0_     from         users.user user0_ 1, Hongten,  123 2, Hanyuan,  5645645 3, Hanyuan,  5645645 4, dfgd,  5645645 5, dfgd,  5645645 Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_ Hibernate:     select         profile0_.id as id1_,         profile0_.user_id as user2_1_,         profile0_.email as email1_,         profile0_.phone as phone1_,         profile0_.mobile as mobile1_,         profile0_.address as address1_,         profile0_.postcode as postcode1_     from         users.profile profile0_ com.b510.example.User com.b510.example.User com.b510.example.User com.b510.example.User com.b510.example.User com.b510.example.Profile com.b510.example.Profile com.b510.example.Profile com.b510.example.Profile com.b510.example.Profile Hibernate:     select         user0_.id as col_0_0_,         user0_.username as col_1_0_,         user0_.password as col_2_0_     from         users.user user0_ 1,  Hongten,  123 2,  Hanyuan,  5645645 3,  Hanyuan,  5645645 4,  dfgd,  5645645 5,  dfgd,  5645645 Hibernate:     select         user0_.id as col_0_0_,         user0_.username as col_1_0_     from         users.user user0_ 1,   Hongten 2,   Hanyuan 3,   Hanyuan 4,   dfgd 5,   dfgd Hibernate:     select         user0_.id as col_0_0_,         user0_.username as col_1_0_,         user0_.password as col_2_0_     from         users.user user0_ 1,  Hongten,  123 2,  Hanyuan,  5645645 3,  Hanyuan,  5645645 4,  dfgd,  5645645 5,  dfgd,  5645645 Hibernate:     select         distinct user0_.username as col_0_0_     from         users.user user0_ Hongten Hanyuan dfgd Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_     where         user0_.id not between 200 and 2000 1Hongten 2Hanyuan 3Hanyuan 4dfgd 5dfgd Hibernate:     select         user0_.username as col_0_0_     from         users.user user0_     where         user0_.id=2 Hanyuan Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_     where         user0_.username in (             'Hongten' , 'Hanyuan' , 'dfgd'         ) 1Hongten 2Hanyuan 3Hanyuan 4dfgd 5dfgd Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_     where         user0_.username not like 'Hon%' 2Hanyuan 3Hanyuan 4dfgd 5dfgd Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_     where         user0_.password is null Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_     where         (             user0_.password is not null         )         and user0_.id<5 1Hongten123 2Hanyuan5645645 3Hanyuan5645645 4dfgd5645645 Hibernate:     select         user0_.id as id0_,         user0_.username as username0_,         user0_.password as password0_     from         users.user user0_     order by         user0_.username,         user0_.id desc 5dfgd 4dfgd 3Hanyuan 2Hanyuan 1Hongten Hibernate:     select         user0_.username as col_0_0_     from         users.user user0_     where         user0_.username=? Hongten

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2011-06-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
HQL 语句
import java.util.Iterator; import java.util.List; import java.util.Map;
Hongten
2018/09/18
8000
Hibernate 中继承映射之二 每个具体类一个表
在数据库中建立连个表:post_delivery和express_delivery
Hongten
2018/09/18
4910
Hibernate4Maven
How to create a Maven project with Hibernate libs? This blog will be a demo to describe this issue.
Hongten
2018/09/13
4840
Hibernate4Maven
Hibernate延迟加载 lazy loading
/**  *  * @author XHW  *  * @date 2011-7-18  *  */ public class HibernateTest {  public static void main(String[] args) {   new HibernateTest().update();  }  public void update(){   Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();   session.beginTransaction();  Category category=(Category)session.get(Category.class, 1);   System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());   Set<Product> products=category.getProducts();   session.getTransaction().commit();    } }
Hongten
2018/09/18
1.1K0
HQL 查询语句
import java.util.Iterator; import java.util.List; import java.util.Map;
Hongten
2018/09/18
8500
Hibernate的性能优化之 dynamic-update dynamic-insert
public class Guestbook implements java.io.Serializable {
Hongten
2018/09/18
6810
Hibernate查询方式之:HQL查询(需要使用的类是:Query)
hibernate查询方式之:HQL查询方式: HQL语句正对的是实体类的名称和实体类的属性进行操作 1.查询所有 格式:from 实体类名称 Query query = session.createQuery(“from User”); List<User> list = query.list(); for(User u :list){ System.out.println(u); } 2.条件查询 (1)from 实体类名称 where 实体类的属性名称=
全栈程序员站长
2022/06/30
9110
Hibernate 中继承映射之三 每一个类一个表
mail:id,redipient,phone,address,postcode             <id><pk>
Hongten
2018/09/18
3960
Hibernate 过滤器
通过调用Session对象的setFilter()和enableFilter()方法使用过滤器。
Hongten
2018/09/18
5770
悲观锁 HibernateTest.java
import org.hibernate.LockMode; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction;
Hongten
2018/09/18
4010
Hibernate中的一级缓存
在一个会话的生命期里面,他所用到的数据会使用缓冲的,第一次读的是编号为2的数据记录
Hongten
2018/09/18
4590
Hibernate 中集合对象的抓取策略(Fetching strategies)
/**  * Product entity. @author MyEclipse Persistence Tools  */
Hongten
2018/09/18
4020
HIbernate的“1+N”问题
import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction;
Hongten
2018/09/18
5480
Hibernate的二级缓存
/**  *  * @author XHW  *  * @date 2011-7-15  *  */ public class HibernateTest {
Hongten
2018/09/18
4770
Hibernate中使用memcached
Hibernate-memcached是CacheProvider接口的memcached实现
Hongten
2018/09/13
6230
hibernate HQL 模糊查询 3
以下是用 hibernate的HQL(面向对象的查询语言)实现模糊查询的3种方式,其中方式一是这三中方式中最理想的方式,至少方式一可以有效的防止由于查询条 件中需要参数的增多导致的代码长度太长出现代码折行的情况(代码太长会给后期的维护和测试带来很大的不便)。但是还有比他更理想的方式,因为方式一在定义 strSQL时使用String ,这就势必会造成当变量过多时strSQL自身太长的问题。一个比较有效的改进办法就是将String 改进为StringBuffer来处理。
py3study
2020/01/08
4940
Hibernate二级缓存存集合对象
新建java project项目:chapter17_setehcache hibernate.cfg.xml 代码: <?xml version='1.0' encoding='UTF-8'?> <
Hongten
2018/09/13
6450
Hibernate之Query接口的uniqueResult()方法[通俗易懂]
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/155613.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/07
5970
【Hibernate】uniqueResult方法「建议收藏」
大家好,又见面了,我是你们的朋友全栈君。 数据库中根据你的查询条件只会返回唯一结果,就可以用uniqueResult这个方法!否则就用list();其返回类型为Object uniqueRes
全栈程序员站长
2022/09/30
4370
day31_Hibernate学习笔记_03
在数据库表中如何表达多对多关系:   使用中间表,分别引用两方的ID。 在对象中如何表达多对多关系:   两方都使用集合表达。 在配置文件中如何表达一对多关系:
黑泽君
2018/10/11
2.5K0
day31_Hibernate学习笔记_03
相关推荐
HQL 语句
更多 >
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文