Hsql 查询_sql数据查询

Hsql 查询_sql数据查询还是班级,学省

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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

Hsql 查询_sql数据查询Hsql 查询_sql数据查询

    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=?

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/190666.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号