Hibernate中HQL查询例子

package com.lanqiao.test;
import java.util.List;
import java.util.Map;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;


import com.lanqiao.domain.Student;
import com.lanqiao.domain.Teacher;
import com.lanqiao.util.HibernateUtil;
public class TestClass {

@Test
public  void testAdd() {
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();
//持久化操作

//保存第一个学生
Student s1 = new Student();
s1.setSname("李四学生");
s1.setAge(25);
session.save(s1);

//保存第一个学生
Student s2 = new Student();
s2.setSname("王五学生");
s2.setAge(35);
session.save(s2);

//保存老师
Teacher  t = new Teacher();
t.setTname("张三老师");
t.setAge(45);
t.getStudent().add(s1);
t.getStudent().add(s2);
session.save(t);

tc.commit();
session.close();
}
/**
* 查询所有的学生信息(HQL)
*/
@Test
public void testQuery1(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();

//定义hql查询结构
String hql = "from Student";
//查询操作
Query query =  session.createQuery(hql);
List students =  query.list();
System.out.println(students.size());
for(Student s : students){
System.out.println(s.getSid()+"\t"+s.getAge()+"\t"+s.getSname()+"\t"+s.getTeacher().getTname());

}
tc.commit();
session.close();
}

/**
* 查询年龄大于30以上的学生(条件查询)
*/
@Test
public void testQuery2(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();

//定义hql查询结构
String hql = "from Student where age>30";
//查询操作
Query query =  session.createQuery(hql);
List students =  query.list();
System.out.println(students.size());
for(Student s : students){
System.out.println(s.getSid()+"\t"+s.getAge()+"\t"+s.getSname()+"\t"+s.getTeacher().getTname());

}
tc.commit();
session.close();
}

/**
* 显示指定的属性(查询单个属性,list集合中的类型为object)
*/
@Test
public void testQuery3(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();

//定义hql查询结构
String hql = "select s.sname from Student s";
//查询操作
Query query =  session.createQuery(hql);
List students =  query.list();
System.out.println(students.size());
for(String sname : students){
System.out.println(sname);
}
tc.commit();
session.close();
}

/**
* 显示指定的属性(查询多个属性,list集合中的类型为Object[]数组类型)
*/
@Test
public void testQuery4(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();

//定义hql查询结构
String hql = "select s.sname,s.age from Student s";
//查询操作
Query query =  session.createQuery(hql);
List students =  query.list();
System.out.println(students.size());
for(Object[] obj : students){
System.out.println(obj[0]);
}
tc.commit();
session.close();
}

/**
* 显示指定的属性(查询多个属性,修改字段的类型,采用对象的构造来完成类型的修改)
*/
@Test
public void testQuery5(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();

//定义hql查询结构
String hql = "select new Student(s.sname,s.age) from Student s";
//查询操作
Query query =  session.createQuery(hql);
List students =  query.list();
System.out.println(students.size());
for(Student s : students){
System.out.println(s.getSname()+"\t"+s.getAge());
}
tc.commit();
session.close();
}

/**
* 显示指定的属性(查询多个属性,map类型:键是别名,值是属性)
*/
@Test
public void testQuery6(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();

//定义hql查询结构
String hql = "select new map(s.sname as name,s.age as age) from Student s";
//查询操作
Query query =  session.createQuery(hql);
List students =  query.list();
System.out.println(students.size());

for (int i = 0; i < students.size(); i++) {
Map map =  students.get(i);
System.out.println(map.get("age"));
}

tc.commit();
session.close();
}

/**
* 条件参数的查询:
* 1、命名参数绑定法
* 2、占位符参数绑定法
*/

@Test
public void testQuery7(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();

//定义hql查询结构
String hql = "from Student where age>:uage and sname=:uname";
//查询操作
Query query =  session.createQuery(hql);
query.setInteger("uage",30);
query.setString("uname", "王五学生");
List students =  query.list();

for (Student s :students) {
System.out.println(s.getSid()+"\t"+s.getSname()+"\t"+s.getAge());
}

tc.commit();
session.close();
}

@Test
public void testQuery8(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();
//定义hql查询结构
String hql = "from Student where age>? and sname=?";
//查询操作
Query query =  session.createQuery(hql);
query.setParameter(0, 20);
query.setParameter(1, "李四学生");
List students =  query.list();
for (Student s :students) {
System.out.println(s.getSid()+"\t"+s.getSname()+"\t"+s.getAge());
}
tc.commit();
session.close();
}
/**
* 删除数据
*/
@Test
public void testQuery9(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();
//定义hql查询结构
String hql = "delete from Student where age>?";
//查询操作
Query query =  session.createQuery(hql);
query.setParameter(0, 30);
query.executeUpdate();//执行(增、删、改操作)
tc.commit();
session.close();
}
/**
* 持久化对象的三种状态
*/
@Test
public void testQuery10(){
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction tc =  session.beginTransaction();

Teacher teacher = new Teacher();//临时状态
teacher.setTname("赵七");
teacher.setAge(55);
session.save(teacher);  //把临时状态对象转换为持久化状态
tc.commit();
session.close();
teacher.setAge(65);//持久化状态转换为游离状态
}




}

你可能感兴趣的:(Hibernate中HQL查询例子)