亲宝软件园·资讯

展开

Hibernate一对多、多对一的关系表达

怀梦想,致远方 人气:1

一、关系表达:

1、一对多、多对一表的关系:

学生表:

 

 

  班级表:

 

 

 在学生表中,学生的学号是主键。在班级表中,班级号是主键,因此,学生表的外键是classno。因此,班级对应学生是一对多,学生对应班级是多对一。因为,一个班级可以有多个学生,但是一个学生只能在一个班级。

2、对象的一对多、多对一关系:

(1)在Class类中,定义Set集合,表达一对多的关系:

 

 

 

package pers.zhb.domain;
import java.util.HashSet;
import java.util.Set;
public class Class {
    private String classno;
    private String department;
    private String monitor;
    private String classname;
    private Set<Student> classes=new HashSet<Student>();//使用set集合表达一对多关系
    public Class(){
    }
    public Set<Student> getClasses() {
        return classes;
    }
    public void setClasses(Set<Student> classes) {
        this.classes = classes;
    }
    public String getMonitor() {
        return monitor;
    }

    public void setMonitor(String monitor) {
        this.monitor = monitor;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }



    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }
    public String getClassno() {
        return classno;
    }

    public void setClassno(String classno) {
        this.classno = classno;
    }
    @Override
    public String toString() {
        return "Class{" +
                "classno='" + classno + '\'' +
                ", department='" + department + '\'' +
                ", monitor='" + monitor + '\'' +
                ", classname='" + classname + '\'' +
                ", classes=" + classes +
                '}';
    }
}
package pers.zhb.domain;
public class Student {
    private Integer studentno;
    private String sname;
    private String sex;
    private String birthday;
    private String classno;
    private Float point;
    private String phone;
    private String email;
    private Clas aClas;
    public Student(){//无参的构造方法
    }
    public Clas getaClas() {
        return aClas;
    }

    public void setaClas(Clas aClas) {
        this.aClas = aClas;
    }
    @Override
    public String toString() {
        return "Student{" +
                "studentno='" + studentno + '\'' +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday='" + birthday + '\'' +
                ", classno='" + classno + '\'' +
                ", point=" + point +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public int getStudentno() {
        return studentno;
    }

    public void setStudentno(int studentno) {
        this.studentno = studentno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getClassno() {
        return classno;
    }

    public void setClassno(String classno) {
        this.classno = classno;
    }

    public float getPoint() {
        return point;
    }

    public void setPoint(float point) {
        this.point = point;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

  

  

(2)定义学生和班级的关系:

 

 

 

package pers.zhb.domain;
import java.util.HashSet;
import java.util.Set;
public class Clas {
private String classno;
private String department;
private String monitor;
private String classname;
private Set<Student> students=new HashSet<Student>();//使用set集合表达一对多关系
public Clas(){
}
public Set<Student> getStudents() {
return students;
}
public void setClasses(Set<Student> students) {
this.students = students;
}
public String getMonitor() {
return monitor;
}

public void setMonitor(String monitor) {
this.monitor = monitor;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}



public String getClassname() {
return classname;
}

public void setClassname(String classname) {
this.classname = classname;
}
public String getClassno() {
return classno;
}

public void setClassno(String classno) {
this.classno = classno;
}
@Override
public String toString() {
return "Class{" +
"classno='" + classno + '\'' +
", department='" + department + '\'' +
", monitor='" + monitor + '\'' +
", classname='" + classname + '\'' +
",students=" + students +
'}';
}
}

3、配置映射文件:

  Class.hbm.xml:

(1)实现一对多的关系映射,即:一个班级对应多个学生:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.orghttps://img.qb5200.com/download-x/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pers.zhb.domain">
<class name="Clas" table="class">
<id name="classno" column="classno">
<generator class="native"></generator>
</id><!--主键-->
<property name="department" column="department"></property>
<property name="monitor" column="monitor"></property>
<property name="classname" column="classname"></property>
<set name="students" table="student"><!--一对多关系配置-->
<key column="classno" update="false"></key><!--指定了集合表的外键-->
<one-to-many class="Student"></one-to-many>
</set>
</class>
</hibernate-mapping>
<set name="students">

指定映射的存储学生的集合的名字。

<key column="classesno"></key>

映射的class表的外键。

<one-to-many class="Student"></one-to-many>

指定学生的类型。

(2)实现多对一的关系映射,即:多个学生对应一个班级。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.orghttps://img.qb5200.com/download-x/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pers.zhb.domain">
<class name="Student" table="student">
<id name="studentno" column="studentno" >
<generator class="native"></generator>
</id>
<property name="birthday" column="birthday"></property>
<property name="classno" column="classno" insert="false" update="false"></property>
<property name="email" column="email"></property>
<property name="phone" column="phone"></property>
<property name="sex" column="sex"></property>
<property name="sname" column="sname"></property>
<property name="point" column="point"></property>
<many-to-one name="aClas" column="classno" class="Clas"></many-to-one>
</class>
</hibernate-mapping>

name属性:映射的班级。

column属性:映射的班级对象对应的外键。

class属性:指定班级的类型。

4、主配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.orghttps://img.qb5200.com/download-x/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <!--配置数据库信息-必须的-->
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stu_mangement</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!--配置hibernate信息-可选的-->
        <property name="hibernate.show_sql">true</property><!--输出底层sql语句-->
        <property name="hibernate.format_sql">true</property><!--格式化输出sql语句-->
        <property name="hibernate.hbm2ddl.auto">update</property><!--hibernate帮助创建表,如果已经有表更新表,如果没有则创建新表-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.connection.isolation">4</property>
        <!--指定session与当前线程绑定-->
        <property name="hibernate.current_session_context_class">thread</property>
        <!--配置数据库的方言,让hibernate识别框架自己的特有语句-->
        <!--把映射文件放到核心配置文件-->
        <mapping resource="pers/zhbhttps://img.qb5200.com/download-x/domain/Student.hbm.xml"/><!--都在src目录下-->
        <mapping resource="pers/zhbhttps://img.qb5200.com/download-x/domain/Class.hbm.xml"/><!--都在src目录下-->
    </session-factory>
</hibernate-configuration>

 二、具体运用:

1、增加:

(1)创建一个新班级并为新班级添加两名学生:

public class Test {
    public static void testSel() {
            Session session = HibernateUtils.openSession();//获得session
            Transaction transaction = session.beginTransaction();//开启事务
            Clas clas=new Clas();
            clas.setClassname("计科171");
            clas.setClassno(4600);
            clas.setDepartment("一号楼");
            clas.setMonitor("zhai");

            Student student=new Student();
            student.setSname("翟");
            student.setStudentno(2017151411);
            student.setPoint(123f);
            student.setSex("男");
            student.setBirthday("2019-11-11");
            student.setPhone("18739496522");
            student.setClassno("221221");
            student.setEmail("34288334@qq.com");

            Student student1=new Student();
            student1.setSname("翟hb");
            student1.setStudentno(2017151419);
            student1.setPoint(666f);
            student1.setSex("女");
            student1.setBirthday("2019-11-11");
            student1.setPhone("18739496522");
            student1.setClassno("221221");
            student1.setEmail("34288334@qq.com");

            clas.getStudents().add(student);//一对多,一个班级下有多个学生
            clas.getStudents().add(student1);//获取Set集合对象并向其中添加元素

            student.setaClas(clas);//多对一,学生属于哪一个班级
            student1.setaClas(clas);

            session.save(clas);
            session.save(student);
            session.save(student1);

            transaction.commit();//提交事务
            session.close();//关闭资源
        }

 

 (2)为一个已经存在的班级添加学生:

 public static void testAdd(){
            Session session = HibernateUtils.openSession();//获得session
            Transaction transaction = session.beginTransaction();//开启事务
            Clas clas=session.get(Clas.class,80501);//获得一个已经存在的班级
            Student student=new Student();//创建一个学生对象
            student.setSname("翟zz");
            student.setStudentno(20190000);
            student.setPoint(133f);
            student.setSex("男");
            student.setBirthday("2019-11-16");
            student.setPhone("18739496522");
            student.setEmail("34288334@qq.com");

            Student student1=new Student();//再创建一个学生对象
            student1.setSname("翟zz");
            student1.setStudentno(20190000);
            student1.setPoint(133f);
            student1.setSex("男");
            student1.setBirthday("2019-11-16");
            student1.setPhone("18739496522");
            student1.setEmail("34288334@qq.com");

            clas.getStudents().add(student);//学生添加到班级
            student.setaClas(clas);//班级与学生对应
            clas.getStudents().add(student1);
            student1.setaClas(clas);

            session.save(student);
            session.save(student1);


            transaction.commit();//提交事务
            session.close();//关闭资源

        }

  

 

 2、删除:

删除80501班的一名学生信息:

 public static void testDel() {
           Session session = HibernateUtils.openSession();//获得session
           Transaction transaction = session.beginTransaction();//开启事务
           Clas clas=session.get(Clas.class,80501);//获得要删除的学生属于那一个班级
           Student student=session.get(Student.class,937221532);//获得要删除的学生
           clas.getStudents().remove(student);
           student.setaClas(null);
           transaction.commit();//提交事务
           session.close();//关闭资源
       }

 

 

 

 

 

 

 

 

加载全部内容

相关教程
猜你喜欢
用户评论