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

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
结果:
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
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 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=?
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/121853.html