加载中...

Hibernate(八)多对多映射


一、创建数据表

  1. --学生证表
  2. create table paper
  3. (
  4. pid number primary key,
  5. pdesc varchar2(100) ,
  6. sid number references student(sid) not null
  7. );
  8. --课程表
  9. create table course
  10. (
  11. cid int primary key,
  12. cname varchar2(50),
  13. cdesc varchar2(200)
  14. );
  15. --学生生和课程表的中间表
  16. create table sc
  17. (
  18. sid number references student(sid),
  19. cid int references course(cid)
  20. );

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

学生类

  1. package entity;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. /*
  5. * 学生类
  6. */
  7. public class Student implements java.io.Serializable {
  8. // Fields
  9.  
  10. private static final long serialVersionUID = 1L;
  11. private int sid;
  12. private String sname;
  13. private String sex;
  14. //增加班级属性
  15. private Grade grade;
  16. //学生证类
  17. private Paper paper;
  18. //添加课程
  19. private Set<Course> courses=new HashSet<Course>();
  20. // Constructors
  21.  
  22. /** default constructor */
  23. public Student() {
  24. }
  25. /** minimal constructor */
  26. public Student(int sid) {
  27. this.sid = sid;
  28. }
  29. /** full constructor */
  30. public Student(int sid, String sname, String sex ) {
  31. this.sid = sid;
  32. this.sname = sname;
  33. this.sex = sex;
  34. }
  35. // Property accessors
  36.  
  37. public int getSid() {
  38. return this.sid;
  39. }
  40. public void setSid(int sid) {
  41. this.sid = sid;
  42. }
  43. public String getSname() {
  44. return this.sname;
  45. }
  46. public void setSname(String sname) {
  47. this.sname = sname;
  48. }
  49. public String getSex() {
  50. return this.sex;
  51. }
  52. public void setSex(String sex) {
  53. this.sex = sex;
  54. }
  55. public Grade getGrade() {
  56. return grade;
  57. }
  58. public void setGrade(Grade grade) {
  59. this.grade = grade;
  60. }
  61. public Paper getPaper() {
  62. return paper;
  63. }
  64. public void setPaper(Paper paper) {
  65. this.paper = paper;
  66. }
  67. public Set<Course> getCourses() {
  68. return courses;
  69. }
  70. public void setCourses(Set<Course> courses) {
  71. this.courses = courses;
  72. }
  73. }
View Code

课程类

  1. package entity;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. /**
  5. * 课程类
  6. */
  7.  
  8. public class Course implements java.io.Serializable {
  9. // Fields
  10.  
  11. /**
  12. *
  13. */
  14. private static final long serialVersionUID = 1L;
  15. private int cid;
  16. private String cname;
  17. private String cdesc;
  18. private Set<Student> students = new HashSet<Student>();
  19. // Constructors
  20.  
  21. /** default constructor */
  22. public Course() {
  23. }
  24. /** minimal constructor */
  25. public Course(int cid) {
  26. this.cid = cid;
  27. }
  28. /** full constructor */
  29. public Course(int cid, String cname, String cdesc, Set <Student>students) {
  30. this.cid = cid;
  31. this.cname = cname;
  32. this.cdesc = cdesc;
  33. this.students = students;
  34. }
  35. // Property accessors
  36.  
  37. public int getCid() {
  38. return this.cid;
  39. }
  40. public void setCid(int cid) {
  41. this.cid = cid;
  42. }
  43. public String getCname() {
  44. return this.cname;
  45. }
  46. public void setCname(String cname) {
  47. this.cname = cname;
  48. }
  49. public String getCdesc() {
  50. return this.cdesc;
  51. }
  52. public void setCdesc(String cdesc) {
  53. this.cdesc = cdesc;
  54. }
  55. public Set<Student> getStudents() {
  56. return students;
  57. }
  58. public void setStudents(Set<Student> students) {
  59. this.students = students;
  60. }
  61. }
View Code

hibernate.cfg.xml

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7.  
  8. <session-factory>
  9. <property name="dialect">
  10. org.hibernate.dialect.Oracle9Dialect
  11. </property>
  12. <property name="connection.url">
  13. jdbc:oracle:thin:@localhost:1521:orcl
  14. </property>
  15. <property name="connection.username">root</property>
  16. <property name="connection.password">root</property>
  17. <property name="connection.driver_class">
  18. oracle.jdbc.OracleDriver
  19. </property>
  20. <property name="show_sql">true</property>
  21. <property name="format_sql">true</property>
  22.  
  23. <mapping resource="entity/Grade.hbm.xml" />
  24. <mapping resource="entity/Student.hbm.xml" />
  25. <mapping resource="entity/Paper.hbm.xml" />
  26. <mapping resource="entity/Course.hbm.xml" />
  27.  
  28. </session-factory>
  29.  
  30. </hibernate-configuration>
View Code

学生类配置文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4.  
  5. <hibernate-mapping>
  6. <class name="entity.Student" table="STUDENT" schema="ROOT">
  7. <id name="sid" type="java.lang.Integer">
  8. <column name="SID" precision="22" scale="0" />
  9. <generator class="assigned" />
  10. </id>
  11. <property name="sname" type="java.lang.String">
  12. <column name="SNAME" length="20" />
  13. </property>
  14. <property name="sex" type="java.lang.String">
  15. <column name="SEX" length="20" />
  16. </property>
  17. <!--配置grade属性 -->
  18. <many-to-one name="grade" class="entity.Grade" cascade="save-update">
  19. <!--指定学生表中的外键 -->
  20. <column name="GID" />
  21. </many-to-one>
  22. <!-- 添加学生证的配置 -->
  23. <one-to-one name="paper" class="entity.Paper" cascade="all" lazy="false" property-ref="student"/>
  24. <!--添加课程 -->
  25. <set name="courses" cascade="save-update" table="SC">
  26. <key column="sid" />
  27. <many-to-many class="entity.Course" column="cid"/>
  28. </set>
  29. </class>
  30. </hibernate-mapping>
View Code

课程类配置文件 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!--
  5. Mapping file autogenerated by MyEclipse Persistence Tools
  6. -->
  7. <hibernate-mapping>
  8. <class name="entity.Course" table="COURSE" schema="ROOT">
  9. <id name="cid" type="java.lang.Integer">
  10. <column name="CID" precision="22" scale="0" />
  11. <generator class="assigned" />
  12. </id>
  13. <property name="cname" type="java.lang.String">
  14. <column name="CNAME" length="50" />
  15. </property>
  16. <property name="cdesc" type="java.lang.String">
  17. <column name="CDESC" length="200" />
  18. </property>
  19. <set name="students" table="SC" cascade="save-update" inverse="true">
  20. <key column="cid" />
  21. <many-to-many class="entity.Student" column="sid" />
  22. </set>
  23. </class>
  24. </hibernate-mapping>
View Code

测试类

  1. package Test;
  2. import java.util.Set;
  3. import org.hibernate.Session;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.cfg.Configuration;
  6. import entity.Course;
  7. import entity.Student;
  8. public class Demo7 {
  9. public static void main(String[] args) {
  10. delte();
  11. }
  12. public static void save(){
  13. Student stu1 = new Student();
  14. stu1.setSid(201503011);
  15. stu1.setSname("赵云");
  16. stu1.setSex("男");
  17. //课程
  18. Course c1=new Course();
  19. c1.setCid(1111);
  20. c1.setCname("长枪阵");
  21. c1.setCdesc("杀进杀出");
  22. Course c2=new Course();
  23. c2.setCid(2222);
  24. c2.setCname("大刀");
  25. c2.setCdesc("青龙偃月刀");
  26. stu1.getCourses().add(c1);
  27. stu1.getCourses().add(c2);
  28. Session session =new Configuration().configure().buildSessionFactory().openSession();
  29. Transaction tran=session.beginTransaction();
  30. session.save(stu1);
  31. tran.commit();
  32. session.close();
  33. }
  34. public static void find(){
  35. Session session =new Configuration().configure().buildSessionFactory().openSession();
  36. Course c=(Course) session.get(Course.class, 1111);
  37. System.out.println(c.getCid()+"\t"+c.getCname()+"\t"+c.getCdesc());
  38. Set<Student> stus=c.getStudents();
  39. for (Student s : stus) {
  40. System.out.println(s.getSname());
  41. }
  42. session.close();
  43. }
  44. public static void update(){
  45. Session session =new Configuration().configure().buildSessionFactory().openSession();
  46. Student stu=(Student) session.get(Student.class, 201509009);
  47. Course cou=(Course) session.get(Course.class, 2222);
  48. Transaction tran=session.beginTransaction();
  49. stu.getCourses().add(cou);
  50. session.update(stu);
  51. tran.commit();
  52. session.close();
  53. }
  54. public static void delte(){
  55. Session session =new Configuration().configure().buildSessionFactory().openSession();
  56. Student stu=(Student) session.get(Student.class, 201509009);
  57. Course cou=(Course) session.get(Course.class, 2222);
  58. Transaction tran=session.beginTransaction();
  59. stu.getCourses().remove(cou);
  60. session.update(stu);
  61. tran.commit();
  62. session.close();
  63. }
  64. }
View Code

 


还没有评论.