日期:2014-05-16  浏览次数:20570 次

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()
    .openSessi