测试Hibernate添加查询修改删除学生信息(一对多的关系)
程序员文章站
2022-07-12 18:09:26
...
一 添加学生和班级测试代码
import com.imooc.util.HibernateUtil;
/*
* 单向一对多关系关系(班级--->学生)
* 建立关联关系后,可以方便的从一个对象导航到另一个对象
* 注意关联的方向
*/
public class Test {
public static void main(String[] args) {
add();
}
//将学生添加到班级
public static void add(){
Grade g=new Grade("Java一班", "Java软件开发一班");
Student stu1=new Student("张三", "男");
Student stu2=new Student("穆女神", "女");
//如果希望在学生表中添加对应的班级编号,需要在班级中添加学生,建立关联关系
g.getStudents().add(stu1);
g.getStudents().add(stu2);
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
session.save(g);
session.save(stu1);
session.save(stu2);
tx.commit();
HibernateUtil.closeSession(session);
}
}
二 测试结果
Hibernate: select max(gid) from grade
Hibernate: select max(sid) from student
Hibernate: insert into grade (gname, gdesc, gid) values (?, ?, ?)
Hibernate: insert into student (sname, sex, sid) values (?, ?, ?)
Hibernate: insert into student (sname, sex, sid) values (?, ?, ?)
Hibernate: update student set gid=? where sid=?
Hibernate: update student set gid=? where sid=?
三 查询班级信息和班级中的学生信息测试代码
package com.imooc.test;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.imooc.entity.Grade;
import com.imooc.entity.Student;
import com.imooc.util.HibernateUtil;
/*
* 单向一对多关系关系(班级--->学生)
* 建立关联关系后,可以方便的从一个对象导航到另一个对象
* 注意关联的方向
*/
public class Test {
public static void main(String[] args) {
findStudentsByGrade();
}
//查询班级中包含的学生
public static void findStudentsByGrade(){
Session session=HibernateUtil.getSession();
Grade grade=(Grade) session.get(Grade.class, 1);
System.out.println(grade.getGname()+","+grade.getGdesc());
Set<Student> students=grade.getStudents();
for(Student stu:students){
System.out.println(stu.getSname()+","+stu.getSex());
}
}
}
四 测试结果
Hibernate: select grade0_.gid as gid1_0_0_, grade0_.gname as gname2_0_0_, grade0_.gdesc as gdesc3_0_0_ from grade grade0_ where grade0_.gid=?
Java一班,Java软件开发一班
Hibernate: select students0_.gid as gid4_0_0_, students0_.sid as sid1_1_0_, students0_.sid as sid1_1_1_, students0_.sname as sname2_1_1_, students0_.sex as sex3_1_1_ from student students0_ where students0_.gid=?
穆女神,女
张三,男
五 修改学生信息测试代码
//修改学生信息
public static void update(){
Grade g=new Grade("Java二班", "Java软件开发二班");
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Student stu=(Student) session.get(Student.class, 1);
g.getStudents().add(stu);
session.save(g);
tx.commit();
HibernateUtil.closeSession(session);
}
六 测试结果
Hibernate: select student0_.sid as sid1_1_0_, student0_.sname as sname2_1_0_, student0_.sex as sex3_1_0_ from student student0_ where student0_.sid=?
Hibernate: select max(gid) from grade
Hibernate: insert into grade (gname, gdesc, gid) values (?, ?, ?)
Hibernate: update student set gid=? where sid=?
七 删除学生信息测试代码
//删除学生信息
public static void delete(){
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Student stu=(Student) session.get(Student.class, 2);
session.delete(stu);
tx.commit();
HibernateUtil.closeSession(session);
}
八 测试结果
Hibernate: select student0_.sid as sid1_1_0_, student0_.sname as sname2_1_0_, student0_.sex as sex3_1_0_ from student student0_ where student0_.sid=?
Hibernate: delete from student where sid=?
上一篇: 创建持久化和映射文件并配置单向一对多关系