HQL语句查询

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

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

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
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.where();
  test.function();
  test.update();
  test.jiaoChaCheck();
  test.innerJoin();
  test.QBC();
  test.leftOuterJoin();
  test.rightOuterJoin();
 }

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();

}

public void function() {// 把大写字母转化为小写字母
  // 作用可以用在:比如在一个用户注册的程序中,大小写不容易区分,但是全部转化为小写后就可以很容易进行比较
  Session session = HibernateSessionFactoryUtil.getSessionFactory()
    .openSession();
  session.beginTransaction();
  // 输出原始的数据
  Query query = session.createQuery("select username from User");
  List<String> list = query.list();

for (String name : list) {
   System.out.println(name);
  }
  System.out.println("-------------------------------------------");
  // 输出的数据全部转化为小写
  query = session.createQuery("select lower(username) from User");
  List<String> listChange = query.list();

for (String name : listChange) {
   System.out.println(name);
  }
  session.getTransaction().commit();
 }

public void update() {
  Session session = HibernateSessionFactoryUtil.getSessionFactory()
    .openSession();
  session.beginTransaction();
  Query query = session
    .createQuery("update User set username=‘洪伟1231‘ where id=?");
  query.setInteger(0, 3);
  int rowCount = query.executeUpdate();
  System.out.println(rowCount);
  session.getTransaction().commit();
 }

public void operateProfile() {// 对profile这个类执行HQL语句操作
  Session session = HibernateSessionFactoryUtil.getSessionFactory()
    .openSession();
  session.beginTransaction();
  // 执行查询操作
  Query query = session.createQuery("from Profile");
  List<Profile> list = query.list();
  for (Profile profile : list) {
   System.out.println(profile.getId() + profile.getEmail()
     + profile.getAddress() + profile.getMobile()
     + profile.getPostcode());
  }
  // 执行删除操作
  query = session.createQuery("delete from Profile where id=?");
  query.setInteger(0, 3);
  int rowCount = query.executeUpdate();
  System.out.println(rowCount);
  session.getTransaction().commit();
 }

public void jiaoChaCheck() {//交叉查询
  //这种方法查询出来的结果是笛卡尔积,对于我们开发中没有多大用处
  Session session = HibernateSessionFactoryUtil.getSessionFactory()
    .openSession();
  session.beginTransaction();
  Query query=session.createQuery("from User,Profile");
  
  List<Object[]> list=query.list();
  
  for(Object[] values:list){
   User user=(User)values[0];
   System.out.print("ID :"+user.getId()+",UserName:"+user.getUsername()+",Password:"+user.getPassword());
   Profile profile=(Profile)values[1];
   System.out.println(profile.getEmail()+profile.getMobile()+profile.getAddress()+profile.getPostcode());
  }
  
  session.getTransaction().commit();
 }

public void innerJoin(){//内连接查询
  /**
   * 下面三种hql语句都是可以得到相同的结果
   * String hql="select p from Profile as p inner join p.user";
   * 在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,这样的查询效率要比上面的hql语句要高
   * String hql="select p from Profile as p inner join fetch p.user";
   * 
   * String hql="select p from Profile p,User u where p.user=u";
   * String hql="select p from Profile p,User u where p.user.id=u.id";
   *  
   */  
  Session session = HibernateSessionFactoryUtil.getSessionFactory()
    .openSession();
  session.beginTransaction();
  String hql="select p from Profile as p inner join fetch p.user";
  //String hql="select p from Profile p,User u where p.user=u";
  //String hql="select p from Profile p,User u where p.user.id=u.id";
  Query query=session.createQuery(hql);
  List<Profile> list=query.list();
  for(Profile p:list){
   System.out.println("ID:"+p.getUser().getId()+"   Username: "+p.getUser().getUsername()+"   Email: "+p.getEmail()+",   Address: "+p.getAddress());
  }
  session.getTransaction().commit();
  }

public void QBC(){//QBC中实现内连接查询
  Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
  session.beginTransaction();
  Criteria criteria=session.createCriteria(Profile.class).createCriteria("user");
  List<Profile> list=criteria.list();
  
  for(Profile p:list){
   System.out.println("ID:"+p.getUser().getId()+"   Username: "+p.getUser().getUsername()+"   Email: "+p.getEmail()+",   Address: "+p.getAddress());
  }
  //QBC中实现外连接
  System.out.println("##################################################");
  criteria=session.createCriteria(Profile.class).setFetchMode("user", FetchMode.JOIN);
  List<Profile> listp=criteria.list();
  
  for(Profile p:list){
   System.out.println("ID:"+p.getUser().getId()+"   Username: "+p.getUser().getUsername()+"   Email: "+p.getEmail()+",   Address: "+p.getAddress());    
  }  
  session.getTransaction().commit();
 }

public void leftOuterJoin(){//左外连接
  /**
   * String hql="select p from Profile p left outer join p.user order by p.user.id";
   * 在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,这样的查询效率要比上面的hql语句要高
   * String hql="select p from Profile p left outer join fetch p.user order by p.user.id";
   *
   * String hqlu="select u from User u left outer join u.profiles";
   *  在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,这样的查询效率要比上面的hql语句要高
   * String hqlu="select u from User u left outer join fetch u.profiles";
   */
  Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  String hql="select p from Profile p left outer join fetch p.user order by p.user.id";
  Query query=session.createQuery(hql);
  
  List<Profile> list=query.list();
  for(Profile p:list){
   System.out.println("ID:"+p.getUser().getId()+"   Username: "+p.getUser().getUsername()+"   Email: "+p.getEmail()+",   Address: "+p.getAddress());
  }
  
  System.out.println("-------------------------------------");
  String hqlu="select u from User u left outer join fetch u.profiles";
  query=session.createQuery(hqlu);
  
  List<User> listu=query.list();
  for(User u:listu){
   System.out.println(u.getId()+u.getUsername()+u.getProfiles());
  }
  session.getTransaction().commit();
   
 }
 
 public void rightOuterJoin(){//右外连接
  Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  String hql="select u from User u right outer join u.profiles order by u.id";
  Query query=session.createQuery(hql);
  
  List<User> listu=query.list();
  for(User user:listu){
   System.out.println(user.getId()+user.getUsername()+user.getProfiles());
  }
  
  session.getTransaction().commit();
   
 }
 
}

结果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
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
3hongwei
4mingliu
5shouzhang
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
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
4mingliu
5shouzhang
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
3hongwei5645645
4mingliu5645645
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
2hanyuan
1hongten
3hongwei
4mingliu
5shouzhang
Hibernate: 
    select
        user0_.username as col_0_0_ 
    from
        users.user user0_ 
    where
        user0_.username=?
hongten
Hibernate: 
    select
        user0_.username as col_0_0_ 
    from
        users.user user0_
hongten
hanyuan
hongwei
mingliu
shouzhang
-------------------------------------------
Hibernate: 
    select
        lower(user0_.username) as col_0_0_ 
    from
        users.user user0_
hongten
hanyuan
hongwei
mingliu
shouzhang
Hibernate: 
    update
        users.user 
    set
        username=‘Hongwei1231‘ 
    where
        id=?
1
Hibernate: 
    select
        user0_.id as id0_0_,
        profile1_.id as id1_1_,
        user0_.username as username0_0_,
        user0_.password as password0_0_,
        profile1_.user_id as user2_1_1_,
        profile1_.email as email1_1_,
        profile1_.phone as phone1_1_,
        profile1_.mobile as mobile1_1_,
        profile1_.address as address1_1_,
        profile1_.postcode as postcode1_1_ 
    from
        users.user user0_,
        users.profile profile1_
ID :1,UserName:hongten,Password:[email protected]
ID :1,UserName:hongten,Password:[email protected]
ID :1,UserName:hongten,Password:[email protected]
ID :2,UserName:hanyuan,Password:[email protected]
ID :2,UserName:hanyuan,Password:[email protected]
ID :2,UserName:hanyuan,Password:[email protected]
ID :3,UserName:Hongwei1231,Password:[email protected]
ID :3,UserName:Hongwei1231,Password:[email protected]
ID :3,UserName:Hongwei1231,Password:[email protected]
ID :4,UserName:mingliu,Password:[email protected]
ID :4,UserName:mingliu,Password:[email protected]
ID :4,UserName:mingliu,Password:[email protected]
ID :5,UserName:shouzhang,Password:[email protected]
ID :5,UserName:shouzhang,Password:[email protected]
ID :5,UserName:shouzhang,Password:[email protected]
Hibernate: 
    select
        profile0_.id as id1_0_,
        user1_.id as id0_1_,
        profile0_.user_id as user2_1_0_,
        profile0_.email as email1_0_,
        profile0_.phone as phone1_0_,
        profile0_.mobile as mobile1_0_,
        profile0_.address as address1_0_,
        profile0_.postcode as postcode1_0_,
        user1_.username as username0_1_,
        user1_.password as password0_1_ 
    from
        users.profile profile0_ 
    inner join
        users.user user1_ 
            on profile0_.user_id=user1_.id
ID:1   Username: hongten   Email: [email protected],   Address: Guangzhoushi
ID:2   Username: hanyuan   Email: [email protected],   Address: GuangzhoushiDianbian
ID:3   Username:Hongwei1231   Email: [email protected],   Address: GuangzhoushiDianbian
Hibernate: 
    select
        this_.id as id1_1_,
        this_.user_id as user2_1_1_,
        this_.email as email1_1_,
        this_.phone as phone1_1_,
        this_.mobile as mobile1_1_,
        this_.address as address1_1_,
        this_.postcode as postcode1_1_,
        user1_.id as id0_0_,
        user1_.username as username0_0_,
        user1_.password as password0_0_ 
    from
        users.profile this_ 
    inner join
        users.user user1_ 
            on this_.user_id=user1_.id
ID:1   Username: hongten   Email: [email protected],   Address: Guangzhoushi
ID:2   Username: hanyuan   Email: [email protected],   Address: GuangzhoushiDianbian
ID:3   Username: Hongwei1231   Email: [email protected],   Address: GuangzhoushiDianbian
##################################################
Hibernate: 
    select
        this_.id as id1_1_,
        this_.user_id as user2_1_1_,
        this_.email as email1_1_,
        this_.phone as phone1_1_,
        this_.mobile as mobile1_1_,
        this_.address as address1_1_,
        this_.postcode as postcode1_1_,
        user2_.id as id0_0_,
        user2_.username as username0_0_,
        user2_.password as password0_0_ 
    from
        users.profile this_ 
    left outer join
        users.user user2_ 
            on this_.user_id=user2_.id
ID:1   Username: hongten   Email: [email protected],   Address: Guangzhoushi
ID:2   Username: hanyuan   Email: [email protected],   Address: GuangzhoushiDianbian
ID:3   Username: 洪伟1231   Email: [email protected],   Address: GuangzhoushiDianbian
Hibernate: 
    select
        profile0_.id as id1_0_,
        user1_.id as id0_1_,
        profile0_.user_id as user2_1_0_,
        profile0_.email as email1_0_,
        profile0_.phone as phone1_0_,
        profile0_.mobile as mobile1_0_,
        profile0_.address as address1_0_,
        profile0_.postcode as postcode1_0_,
        user1_.username as username0_1_,
        user1_.password as password0_1_ 
    from
        users.profile profile0_ 
    left outer join
        users.user user1_ 
            on profile0_.user_id=user1_.id 
    order by
        profile0_.user_id
ID:1   Username: hongten   Email: [email protected],   Address: Guangzhoushi
ID:2   Username: hanyuan   Email: [email protected],   Address: GuangzhoushiDianbian
ID:3   Username: 洪伟1231   Email: [email protected],   Address: GuangzhoushiDianbian
-------------------------------------
Hibernate: 
    select
        user0_.id as id0_0_,
        profiles1_.id as id1_1_,
        user0_.username as username0_0_,
        user0_.password as password0_0_,
        profiles1_.user_id as user2_1_1_,
        profiles1_.email as email1_1_,
        profiles1_.phone as phone1_1_,
        profiles1_.mobile as mobile1_1_,
        profiles1_.address as address1_1_,
        profiles1_.postcode as postcode1_1_,
        profiles1_.user_id as user2_0__,
        profiles1_.id as id0__ 
    from
        users.user user0_ 
    left outer join
        users.profile profiles1_ 
            on user0_.id=profiles1_.user_id
1hongten[[email protected]]
2hanyuan[[email protected]]
3Hongwei1231[[email protected]]
4mingliu[]
5shouzhang[]
Hibernate: 
    select
        user0_.id as id0_,
        user0_.username as username0_,
        user0_.password as password0_ 
    from
        users.user user0_ 
    right outer join
        users.profile profiles1_ 
            on user0_.id=profiles1_.user_id 
    order by
        user0_.id
Hibernate: 
    select
        profiles0_.user_id as user2_1_,
        profiles0_.id as id1_,
        profiles0_.id as id1_0_,
        profiles0_.user_id as user2_1_0_,
        profiles0_.email as email1_0_,
        profiles0_.phone as phone1_0_,
        profiles0_.mobile as mobile1_0_,
        profiles0_.address as address1_0_,
        profiles0_.postcode as postcode1_0_ 
    from
        users.profile profiles0_ 
    where
        profiles0_.user_id=?
1hongten[[email protected]]
Hibernate: 
    select
        profiles0_.user_id as user2_1_,
        profiles0_.id as id1_,
        profiles0_.id as id1_0_,
        profiles0_.user_id as user2_1_0_,
        profiles0_.email as email1_0_,
        profiles0_.phone as phone1_0_,
        profiles0_.mobile as mobile1_0_,
        profiles0_.address as address1_0_,
        profiles0_.postcode as postcode1_0_ 
    from
        users.profile profiles0_ 
    where
        profiles0_.user_id=?
2hanyuan[[email protected]]
Hibernate: 
    select
        profiles0_.user_id as user2_1_,
        profiles0_.id as id1_,
        profiles0_.id as id1_0_,
        profiles0_.user_id as user2_1_0_,
        profiles0_.email as email1_0_,
        profiles0_.phone as phone1_0_,
        profiles0_.mobile as mobile1_0_,
        profiles0_.address as address1_0_,
        profiles0_.postcode as postcode1_0_ 
    from
        users.profile profiles0_ 
    where
        profiles0_.user_id=?
3Hongwei1231[[email protected]]

时间: 2024-10-31 22:56:59

HQL语句查询的相关文章

关于在SSH框架整合时,使用hql语句查询出现 XXX is not mapped 的问题

在做SSH框架的整合过程中,新手往往会遇到这个问题.当我们使用hql语句查询时,控制台报错: XXX is not mapped.而此时,很多觉得英语不错的同学就会被自己搞晕.然后开始检查实体类的映射文件*.hbm.xml是否正确,是否将映射文件添加到spring的配置文件中,等等.然后高亮一通,仍不见效. 我们开始正题.大家都知道,Hibernate是一个让我们完全面向对象编程的框架.也就是说,我们对数据库数据进行操作的时候,实际是通过实体类.在Hibernate中的查询方式有3种: (1)通

在hibernate3中如何利用HQL语句查询出对象中的部分数据并且返回该对象?

例如现在有一个Customer对象 public class Customer{ private Integer cid; private String cname; private Integer cage; } 现在说在hibernate中利用HQL查找出了cname一列值,然后将其封装成一个对象,问HQL要怎么写(投影操作)? 解决:1.在Customer实体类中添加一个带参的构造方法 public class Customer{ public Customer(){ super(); }

hibernate之HQL语句

1. 什么是hql HQL是Hibernate Query Language的缩写 2. hql和sql区别/异同 HQL                                          SQL 类名/属性                                     表名/列名 区分大小写,关键字不区分大小写                   不区分大小 别名                                                别名 ?,

hql语句的模糊查询

代码如下: public List<Teacher> findTeacher(String str) { String hql="from Teacher where 1=1"; hql+=" and tname like '%"+str+"%'"+" or tcode like '%"+str+"%'"; Query query=getSession().createQuery(hql); L

使用HQL语句的按照参数名字查询数据库信息的时候 “=:”和参数之间不能存在空格,否则会报错

问题描述: 今天在使用HQL的按照参数的名字查询数据库信息的时候报错如下: org.hibernate.QueryException: Space is not allowed after parameter prefix ':' [from Users user where user.address=: userAddress] at org.hibernate.engine.query.spi.ParameterParser.parse(ParameterParser.java:95) at

[Hibernate] 通过 properties 类和 hql 语句进行动态查询

//需要保证Emp和EmpProperties中的setter和getter以及属性以及 参数占位符(:eName) 的一致//动态查询 @Test public void test4(){ EmpProperties ep=new EmpProperties(); //set query conditions ep.seteName("%A%"); ep.setStartDate(new Date(383155200000L)); ep.setEndDate(new Date());

hibernate 之 HQL语句总结【转】

1. 查询整个映射对象所有字段 //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 String hql = "from Users"; Query query = session.createQuery(hql); List<Users> users = query.list(); for(Users user : users){ System.out.println(user.getName() + " : " + user.ge

hibernate学习系列-----(4)hibernate基本查询上篇:HQL基本查询

紧接着上一篇,今天继续hibernate的学习总结,来聊一聊hibernate的基本查询方法,先说说HQL(hibernate Query Language):它是官方推荐的查询语言.在开始写代码之前,看看需要做哪些准备工作吧,首先,在我们的学生类中新增一个属性"clazz",其实不加也可以,接着,我们需要重写Student.java类中的toString()方法,代码如下: /** * 重写toString方法 */ @Override public String toString(

HQL语句使用

1.查询结果是表的部分字段,而不是全部 ad是CouponAd 对象 coupon 是ad的属性,也是一个实体类 select ad.coupon from CouponAd ad where ad.deleted=false Order by createdDate desc 2.like模糊搜索 这里变量key是String类型. String hql = "from Coupon coupon where coupon.deleted=false and coupon.name like