hibernate多对多

来源:互联网 发布:js单引号转义字符 编辑:程序博客网 时间:2024/04/29 17:07

SQL:

CREATE TABLE TB_STUDENT(    ID INTEGER PRIMARY KEY,    NAME VARCHAR2(20) NOT NULL);

CREATE SEQUENCE SQ_STUDENTINCREMENT BY 1START WITH 1NOMAXVALUENOCYCLECACHE 10;

CREATE TABLE TB_COURSE(    ID INTEGER PRIMARY KEY,    NAME VARCHAR2(20) NOT NULL);

CREATE SEQUENCE SQ_COURSEINCREMENT BY 1START WITH 1NOMAXVALUENOCYCLECACHE 10;

CREATE TABLE TB_STUDENT_COURSE(    STUDENT_ID INTEGER NOT NULL REFERENCES TB_STUDENT(ID),    COURSE_ID INTEGER NOT NULL REFERENCES TB_COURSE(ID),    PRIMARY KEY(STUDENT_ID, COURSE_ID));

java代码:

package com.many2many.pojo;import java.util.Set;public class Course {private int id;private String name;private Set<Student> studentSet;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Student> getStudentSet() {return studentSet;}public void setStudentSet(Set<Student> studentSet) {this.studentSet = studentSet;}}

package com.many2many.pojo;import java.util.Set;public class Student {private int id;private String name;private Set<Course> courseSet;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Course> getCourseSet() {return courseSet;}public void setCourseSet(Set<Course> courseSet) {this.courseSet = courseSet;}}

hbm:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.many2many.pojo"><class name="Course" table="tb_course"><id name="id" column="id" type="integer"><generator class="sequence"><param name="sequence">SQ_COURSE</param></generator></id><property name="name" column="name" type="string" /><set name="studentSet" table="tb_student_course" cascade="save-update"><key column="course_id"></key><many-to-many class="Student" column="student_id"></many-to-many></set></class></hibernate-mapping>

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.many2many.pojo"><class name="Student" table="tb_student"><id name="id" column="id" type="integer"><generator class="sequence"><param name="sequence">SQ_STUDENT</param></generator></id><property name="name" column="name" type="string" /><set name="courseSet" table="tb_student_course" cascade="save-update"><key column="student_id"></key><many-to-many class="Course" column="course_id"></many-to-many></set></class></hibernate-mapping>

oracle.hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- Database connection settings --><property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property><property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property><property name="connection.username">HIBERNATE</property><property name="connection.password">HIBERNATE</property><property name="connection.pool_size">1</property><property name="dialect">org.hibernate.dialect.Oracle10gDialect</property><property name="show_sql">true</property><property name="hibernate.show_sql">true </property><property name="jdbc.fetch_size">50</property><mapping resource="com/many2many/pojo/Course.hbm.xml"/><mapping resource="com/many2many/pojo/Student.hbm.xml"/></session-factory></hibernate-configuration>

测试代码:

package com.many2many.test;import java.util.HashSet;import java.util.Set;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.many2many.pojo.Course;import com.many2many.pojo.Student;import junit.framework.TestCase;public class TestMany2Many extends TestCase {private Session session = null;private static final String cfgName = "/oracle.hibernate.cfg.xml";private static final SessionFactory factory = new Configuration().configure(cfgName).buildSessionFactory();private Transaction tran = null;private static final String[] courseNames = new String[] { "语文", "数学", "英语", "生物", "化学", "物理", "历史", "地理", "政治" };private static final String[] studentNames = new String[] {"赵云", "关羽", "刘备", "诸葛亮", "曹操", "司马懿", "孙权", "张飞", "周瑜"};@Overrideprotected void setUp() throws Exception {session = factory.openSession();tran = session.beginTransaction();}public void testInsert() {Set<Course> courseSet = new HashSet<Course>();Set<Student> studentSet = new HashSet<Student>();for (String courseName: courseNames) {Course course = new Course();course.setName(courseName);courseSet.add(course);}for (String studentName: studentNames) {Student student = new Student();student.setName(studentName);studentSet.add(student);}for (Student student: studentSet) {student.setCourseSet(courseSet);session.save(student);}//保存student时自动会保存course,所以下面的代码,必须注释掉//for (Course course: courseSet) {//course.setStudentSet(studentSet);//session.save(course);//}tran.commit();}@Overrideprotected void tearDown() throws Exception {tran = null;session.close();session = null;}}

查询数据库:

SQL> SELECT * FROM TB_STUDENT;        ID NAME---------- --------------------        55 张飞        56 司马懿        57 关羽        58 孙权        59 周瑜        60 曹操        61 诸葛亮        62 赵云        63 刘备已选择9行。SQL> SELECT * FROM TB_COURSE;        ID NAME---------- --------------------        37 语文        38 地理        39 英语        40 历史        41 物理        42 政治        43 化学        44 生物        45 数学已选择9行。SQL> SELECT * FROM TB_STUDENT_COURSE;STUDENT_ID  COURSE_ID---------- ----------        55         37        55         38        55         39        55         40        55         41        55         42        55         43        55         44        55         45        56         37        56         38STUDENT_ID  COURSE_ID---------- ----------        56         39        56         40        56         41        56         42        56         43        56         44        56         45        57         37        57         38        57         39        57         40STUDENT_ID  COURSE_ID---------- ----------        57         41        57         42        57         43        57         44        57         45        58         37        58         38        58         39        58         40        58         41        58         42STUDENT_ID  COURSE_ID---------- ----------        58         43        58         44        58         45        59         37        59         38        59         39        59         40        59         41        59         42        59         43        59         44STUDENT_ID  COURSE_ID---------- ----------        59         45        60         37        60         38        60         39        60         40        60         41        60         42        60         43        60         44        60         45        61         37STUDENT_ID  COURSE_ID---------- ----------        61         38        61         39        61         40        61         41        61         42        61         43        61         44        61         45        62         37        62         38        62         39STUDENT_ID  COURSE_ID---------- ----------        62         40        62         41        62         42        62         43        62         44        62         45        63         37        63         38        63         39        63         40        63         41STUDENT_ID  COURSE_ID---------- ----------        63         42        63         43        63         44        63         45已选择81行。SQL>


0 0