Hsql 查询

来源:互联网 发布:大数据安全监控 编辑:程序博客网 时间:2024/05/16 09:32

                       还是班级,学生,课程,学生选课,四张表初始值:

    1,查询所有的学生

session.createQuery("from Student s").list()

sql语句:

Hibernate:     select        student0_.stu_id as stu1_0_,        student0_.class_id as class5_0_,        student0_.stu_birth as stu2_0_,        student0_.stu_name as stu3_0_,        student0_.stuSex as stuSex0_     from        tb_student student0_


2,将学生表和课程表做隐式连接查询:

session.createQuery("from Student s  where s.stuCourses.stuCourseId>:id").setInteger("id", 0).list();


出现异常: org.hibernate.QueryException: illegal attempt to dereference collection原因是在Hibernate 3.0 以后对关联实体是集合的属性,不能直接使用隐式连接查询,改成:

Session session=HibernateSessionFactory.getSession();List<Object[]> scs=session.createQuery("from Student s inner join s.stuCourses sc where sc.stuCourseId>:id").setInteger("id", 0).list();//List<Object[]> scs=session.createQuery("from Student s  where s.stuCourses.stuCourseId>:id").setInteger("id", 0).list();for(Object[] o:scs){Student s=(Student)o[0];StuCourse sc= (StuCourse)o[1];System.out.println("stuName:"+s.getStuName()+"stuCourseName:"+sc.getCourse().getCourseName());}


结果:

Hibernate:     select        student0_.stu_id as stu1_0_0_,        stucourses1_.sc_id as sc1_2_1_,        student0_.class_id as class5_0_0_,        student0_.stu_birth as stu2_0_0_,        student0_.stu_name as stu3_0_0_,        student0_.stuSex as stuSex0_0_,        stucourses1_.course_id as course4_2_1_,        stucourses1_.sc_score as sc2_2_1_,        stucourses1_.sc_time as sc3_2_1_,        stucourses1_.stu_id as stu5_2_1_     from        tb_student student0_     inner join        tb_stuCourse stucourses1_             on student0_.stu_id=stucourses1_.stu_id     where        stucourses1_.sc_id>?


生成的集合的元素,是由Student,和StuCourse两个实体组成的数组,如果只需要保留一个实体  hsql改成:

session.createQuery("select s from Student s inner join s.stuCourses sc where sc.stuCourseId>:id").setInteger("id", 0).list();


3,使用 left/right out join 查询:

session.createQuery("select s from Student s left outer join s.stuCourses sc").list();

生成的sql语句:

Hibernate:     select        student0_.stu_id as stu1_0_,        student0_.class_id as class5_0_,        student0_.stu_birth as stu2_0_,        student0_.stu_name as stu3_0_,        student0_.stuSex as stuSex0_     from        tb_student student0_     left outer join        tb_stuCourse stucourses1_             on student0_.stu_id=stucourses1_.stu_id

right outer join:

session.createQuery("select s from Student s right outer join s.stuCourses sc").list();

sql:

Hibernate:     select        student0_.stu_id as stu1_0_,        student0_.class_id as class5_0_,        student0_.stu_birth as stu2_0_,        student0_.stu_name as stu3_0_,        student0_.stuSex as stuSex0_     from        tb_student student0_     right outer join        tb_stuCourse stucourses1_             on student0_.stu_id=stucourses1_.stu_id


4,使用分页查询:

(List<Student>)session.createQuery("from Student s").setFirstResult(1).setMaxResults(2).list();

setFirstResult()从哪条记录开始,setMaxResult()每次取多少条记录

Hibernate:     select        student0_.stu_id as stu1_0_,        student0_.class_id as class5_0_,        student0_.stu_birth as stu2_0_,        student0_.stu_name as stu3_0_,        student0_.stuSex as stuSex0_     from        tb_student student0_ limit ?,        ?


在MySql里面调用的还是 limit 关键字 来进行分页

5,使用条件查询:

//学生根据班级,性别,出生年月进行查询public List<Student> getRightStudent(){// 使用条件查询Session session=HibernateSessionFactory.getSession();return session.createCriteria(Student.class).add(Restrictions.eq("stuSex", true)).createCriteria("myClass").add(Restrictions.eq("classId", 1)).list();}

注意,虽然添加了导航属性“myClass”  但最终结果的元素 还是 Student 实体 看sql:

Hibernate:     select        this_.stu_id as stu1_0_1_,        this_.class_id as class5_0_1_,        this_.stu_birth as stu2_0_1_,        this_.stu_name as stu3_0_1_,        this_.stuSex as stuSex0_1_,        theclass1_.classId as classId1_0_,        theclass1_.className as className1_0_     from        tb_student this_     inner join        tb_class theclass1_             on this_.class_id=theclass1_.classId     where        this_.stuSex=?         and theclass1_.classId=?


 



 


 

0 0
原创粉丝点击