配置hibernate环境(https://blog.csdn.net/weixin_43317111/article/details/103078545)
实体类
package com.wd.pojo;
public class Student {
private Integer id;
private String name;
private Teacher tid;//注意映射到Teacher的主键,必须为Teacher类型
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTid() {
return tid;
}
public void setTid(Teacher tid) {
this.tid = tid;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Student(Integer id, String name, Teacher tid) {
super();
this.id = id;
this.name = name;
this.tid = tid;
}
public Student(String name, Teacher tid) {
super();
this.name = name;
this.tid = tid;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
package com.wd.pojo;
public class Teacher {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
}
public Teacher(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Teacher(String name) {
super();
this.name = name;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
}
映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.wd.pojo.Student" table="t_student">
<id name="id" column="id">
<generator class="identity"></generator>
</id>
<property name="name" column="name" length="50"></property>
<many-to-one name="tid" column="tid" not-null="true"></many-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.wd.pojo.Teacher" table="t_teacher">
<id name="id" column="id">
<generator class="identity"></generator>
</id>
<property name="name" column="name" length="50" ></property>
</class>
</hibernate-mapping>
测试代码:
- Session生命周期(游离态,持久态,瞬时态)
- 测试save,delete,update,get
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否打印脚本,第二个参数是否导入数据库
se.create(true, true);
}
@Test
public void testInit(){
//准备数据
Teacher teacher1 = new Teacher("qwer");
Teacher teacher2 = new Teacher("asdf");
Student student1 = new Student("张三", teacher1);
Student student2 = new Student("李四", teacher2);
Student student3 = new Student("王五", teacher2);
Student student4 = new Student("赵柳", teacher2);
Session session = null;
try {
session = HibernateUtil.getSession();
//开启事务,若其中某一步操作失败则回滚到初始状态
session.beginTransaction();
//此处必须先保存teacher,因为student的属性依赖于teacher
session.save(teacher1);
session.save(teacher2);
session.save(student1);
session.save(student2);
session.save(student3);
session.save(student4);
//提交事务
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
//出现异常进行事务回滚
session.getTransaction().rollback();
}finally {
if(session!=null){
session.close();
}
}
}
@Test
public void testGet(){
Session session=null;
try {
session = HibernateUtil.getSession();
session.beginTransaction();
//即时查询,直接生成sql语句,不管是否在使用,若查询不存在则返回null
// Student stu = (Student) session.get(Student.class, 2);
// System.out.println(stu);
//此时并不会生成sql语句并且对t_teacher表进行查询
Student stu1 = (Student) session.load(Student.class, 0);
//在使用时才会生成sql语句并且对t_teacher表进行查询,若查询不存在则报异常
System.out.println(stu1.getTid());
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testSession(){
//游离状态
//持久状态
//瞬时状态
Session session=null;
try {
session = HibernateUtil.getSession();
session.beginTransaction();
Student student = (Student) session.get(Student.class, 3);
System.out.println(student);
Teacher teacher = new Teacher(2,"asdf");
student.setTid(teacher);//处于持久状态时,不需要进行save()便能对数据进行更改
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
if(session!=null){
session.close();
}
}
}
@Test
public void testSave(){
Session session=null;
try {
session = HibernateUtil.getSession();
session.beginTransaction();
//因为t_student的tid字段依赖于t_teacher,所以在进行学生信息保存的时候必须先进行teacher信息的保存
Teacher teacher1 = new Teacher("tyu");//新创建的teacher
session.save(teacher1);
Student student1 = new Student("Steven", teacher1);
session.save(student1);
System.out.println("===========================");
Teacher teacher2 = (Teacher) session.get(Teacher.class, 1);//在数据库中已经保存的teacher
Student student2 = new Student("Russell", teacher2);
session.save(student2);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
if(session!=null){
session.close();
}
}
}
@Test
public void testDel(){
Session session = null;
try {
session= HibernateUtil.getSession();
session.beginTransaction();
// Teacher teacher = new Teacher("zxcv"); //此时没有主键,判断为瞬时状态,无法进行删除,必须要有主键
Teacher teacher = new Teacher(3, "zx");
session.delete(teacher); //按照主键进行删除,尽管其他字段与表格中不符合,也能够删除成功
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testUpd(){
Session session= null;
try {
session= HibernateUtil.getSession();
session.beginTransaction();
Teacher teacher = new Teacher(4,"tyui");
//必须为持久态才能够进行(也就是必须要在数据库中存在且提供主键)
//若是被其他表所依赖,只要被依赖字段不改变,则在更新其他字段的时候,并不会产生影响
session.update(teacher);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}