1、包结构:
2、源代码:
Course.java
package domain;
import java.util.HashSet;
import java.util.Set;
public class Course {
private String id;
private String cid;
private String course;
private Set<Student> students=new HashSet<Student>();
public Course() {
}
public Course(String cid, String course) {
this.cid = cid;
this.course = course;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
Student.java:
package domain;
import java.util.HashSet;
import java.util.Set;
public class Student {
private String id;
private String sid;
private String name;
private Set<Course> courses=new HashSet<Course>();
public Student(){};
public Student(String sid,String name){
this.sid=sid;
this.name=name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
HbnUtil.java:
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HbnUtil {
private static SessionFactory sessionFactory;
private HbnUtil(){}
public static synchronized SessionFactory getSessionFactory(){
if(sessionFactory==null){
sessionFactory=new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
}
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:myorcl</property>
<property name="connection.username">myorcl</property>
<property name="connection.password">embed</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="myeclipse.connection.profile">myorcl</property>
<!--<property name="hibernate.connection.release_mode">AFTER_TRANSACTION</property>
-->
<property name="show_sql">true</property>
<mapping resource="domain/Student.hbm.xml"/>
<mapping resource="domain/Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Course.hbm.xml
<?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 package="domain">
<class name="Course" table="COURSE">
<id name="id" column="id">
<generator class="uuid"></generator>
</id>
<property name="cid" column="cid"></property>
<property name="course" column="course"></property>
<set name="students" table="SC" inverse="true">
<key column="CFK"></key>
<many-to-many class="Student" column="SFK"></many-to-many>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml:
<?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 package="domain">
<class name="Student" table="STUDENT">
<id name="id" column="ID">
<generator class="uuid"></generator>
</id>
<property name="sid" column="SID"></property>
<property name="name" column="NAME"></property>
<set name="courses" table="SC" cascade="all">
<key column="SFK"></key>
<many-to-many class="Course" column="CFK"></many-to-many>
</set>
</class>
</hibernate-mapping>
Test.java:
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HbnUtil;
import domain.Course;
import domain.Student;
public class Test {
public static void main(String[] args){
SessionFactory sf=HbnUtil.getSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
Student s1=new Student("sid4","li");
Course c1=new Course("cid4","C++");
Student s2=new Student("sid5","li");
Course c2=new Course("cid5","C++");
s1.getCourses().add(c1);
s1.getCourses().add(c2);
c2.getStudents().add(s2);
//session.save(s1);
//session.save(c1);
//session.save(c2);
//session.save(s2);
Object obj=session.get(Student.class, "4028e4282e091aa1012e091aa5420001");
session.delete(obj);
session.flush();
session.getTransaction().commit();
session.close();
sf.close();
}
}
以上代码使用中间表SC实现了多对多关系映射,但我还有一个疑问:就是如果中间表SC还想加入一个score(学生分数)字段,该如何处理呢?网上有人说将多对多映射拆成两个一对多映射。大家都是这么做的么?