加载中...

Hibernate(八)多对多映射


一、创建数据表

--学生证表
create table paper
(
       pid   number primary key,
       pdesc varchar2(100)  ,
       sid   number references student(sid) not null
       
);
--课程表
create table course
(
       cid                     int          primary key,
       cname                   varchar2(50),
       cdesc                   varchar2(200)
);
--学生生和课程表的中间表
create table sc
(
       sid   number            references student(sid),
       cid   int                references course(cid)
);

二、创建持久化类和配置文件

学生类

package entity;

import java.util.HashSet;
import java.util.Set;

/*
 * 学生类
 */
public class Student implements java.io.Serializable {

    // Fields

    private static final long serialVersionUID = 1L;
    private int sid;
    private String sname;
    private String sex;
    //增加班级属性
    private Grade grade;
    //学生证类
    private Paper paper;
    //添加课程
    private Set<Course> courses=new HashSet<Course>();

    // Constructors

    /** default constructor */
    public Student() {
    }

    /** minimal constructor */
    public Student(int sid) {
        this.sid = sid;
    }

    /** full constructor */
    public Student(int sid, String sname, String sex ) {
    
        this.sid = sid;

        this.sname = sname;
        this.sex = sex;
    }

    // Property accessors

    public int getSid() {
        return this.sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    

    public String getSname() {
        return this.sname;
    }

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

    public String getSex() {
        return this.sex;
    }

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

    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade grade) {
        this.grade = grade;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public Set<Course> getCourses() {
        return courses;
    }

    public void setCourses(Set<Course> courses) {
        this.courses = courses;
    }

    
}
View Code

课程类

package entity;

import java.util.HashSet;
import java.util.Set;

/**
 * 课程类
 */

public class Course implements java.io.Serializable {

    // Fields

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int cid;
    private String cname;
    private String cdesc;
    private Set<Student> students = new HashSet<Student>();

    // Constructors

    /** default constructor */
    public Course() {
    }

    /** minimal constructor */
    public Course(int cid) {
        this.cid = cid;
    }

    /** full constructor */
    public Course(int cid, String cname, String cdesc, Set <Student>students) {
        this.cid = cid;
        this.cname = cname;
        this.cdesc = cdesc;
        this.students = students;
    }

    // Property accessors

    public int getCid() {
        return this.cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getCname() {
        return this.cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCdesc() {
        return this.cdesc;
    }

    public void setCdesc(String cdesc) {
        this.cdesc = cdesc;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }

    

}
View Code

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.Oracle9Dialect
        </property>
        <property name="connection.url">
            jdbc:oracle:thin:@localhost:1521:orcl
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">
            oracle.jdbc.OracleDriver
        </property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <mapping resource="entity/Grade.hbm.xml" />
        <mapping resource="entity/Student.hbm.xml" />
        <mapping resource="entity/Paper.hbm.xml" />
        <mapping resource="entity/Course.hbm.xml" />

    </session-factory>

</hibernate-configuration>
View Code

学生类配置文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="entity.Student" table="STUDENT" schema="ROOT">
        <id name="sid" type="java.lang.Integer">
            <column name="SID" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" length="20" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="SEX" length="20" />
        </property>
        <!--配置grade属性  -->
        <many-to-one name="grade" class="entity.Grade" cascade="save-update">
        <!--指定学生表中的外键  -->
        <column name="GID" />
        </many-to-one>
        <!-- 添加学生证的配置 -->
        <one-to-one name="paper" class="entity.Paper" cascade="all" lazy="false" property-ref="student"/>
        <!--添加课程  -->
           <set name="courses" cascade="save-update" table="SC">
           <key column="sid" />
           <many-to-many class="entity.Course" column="cid"/>
           </set>
    </class>
</hibernate-mapping>
View Code

课程类配置文件 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.Course" table="COURSE" schema="ROOT">
        <id name="cid" type="java.lang.Integer">
            <column name="CID" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="cname" type="java.lang.String">
            <column name="CNAME" length="50" />
        </property>
        <property name="cdesc" type="java.lang.String">
            <column name="CDESC" length="200" />
        </property>
        <set name="students"  table="SC" cascade="save-update" inverse="true">
            <key column="cid" />
            <many-to-many class="entity.Student" column="sid" />
        </set>
    </class>
</hibernate-mapping>
View Code

测试类

package Test;

import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import entity.Course;
import entity.Student;

public class Demo7 {

    public static void main(String[] args) {
        delte();

    }
    public static void save(){
        Student stu1 = new Student();
        stu1.setSid(201503011);
        stu1.setSname("赵云");
        stu1.setSex("男");
        
        //课程
        Course c1=new Course();
        c1.setCid(1111);
        c1.setCname("长枪阵");
        c1.setCdesc("杀进杀出");
        
        Course c2=new Course();
        c2.setCid(2222);
        c2.setCname("大刀");
        c2.setCdesc("青龙偃月刀");
        
        stu1.getCourses().add(c1);
        stu1.getCourses().add(c2);
        
        
        Session session =new Configuration().configure().buildSessionFactory().openSession();
        Transaction tran=session.beginTransaction();
        session.save(stu1);
        
        tran.commit();
        session.close();
    }

    public static void find(){
        
        Session session =new Configuration().configure().buildSessionFactory().openSession();
        Course c=(Course) session.get(Course.class, 1111);
        System.out.println(c.getCid()+"\t"+c.getCname()+"\t"+c.getCdesc());
        Set<Student> stus=c.getStudents();
        for (Student s : stus) {
            System.out.println(s.getSname());
        }
        session.close();
    }
    
    public static void update(){

        Session session =new Configuration().configure().buildSessionFactory().openSession();
        Student stu=(Student) session.get(Student.class, 201509009);
        
        Course cou=(Course) session.get(Course.class, 2222);
        
        Transaction tran=session.beginTransaction();
        stu.getCourses().add(cou);
        session.update(stu);
        tran.commit();
        session.close();
    }
    public static void delte(){
        Session session =new Configuration().configure().buildSessionFactory().openSession();
        Student stu=(Student) session.get(Student.class, 201509009);
        
        Course cou=(Course) session.get(Course.class, 2222);
        
        Transaction tran=session.beginTransaction();
        stu.getCourses().remove(cou);
        session.update(stu);
        tran.commit();
        session.close();
    }

}
View Code

 


还没有评论.