hibernate配置文件
<?xml version="1.0"?>
<!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="show_sql">true</property>
<property name="connection.isolation">2</property>
<property name="myeclipse.connection.profile">mysqlyuan</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="hbm2ddl.auto">create</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/tarena/enrollment/biz/entity/Student.hbm.xml" />
<mapping resource="com/tarena/enrollment/biz/entity/Course.hbm.xml" />
</session-factory>
</hibernate-configuration>
Course.hbm.xml关系映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.tarena.enrollment.biz.entity">
<class name="Course" table="t_course_many2many" >
<id name="oid"
column="OID">
<generator class="native"/>
</id>
<property name="cid"
column="CID"
unique="true"
not-null="true"/>
<property name="name"
column="NAME"
not-null="true" />
<set name="stus" table="t_enrollment"
cascade="save-update">
<key column="CFID"/>
<many-to-many class="Student"
column="SFID"/>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml关系映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.tarena.enrollment.biz.entity">
<class name="Student" table="s_student_man2many">
<id name="oid" column="OID">
<generator class="native"/>
</id>
<property name="sid"
column="STUDENTID"
unique="true"
not-null="true"/>
<property name="name"
column="STUDENTNAME"
not-null="true"/>
<set name="cours" table="t_enrollment"
cascade="save-update"
inverse="true">
<key column="SFID"/>
<many-to-many class="Course"
column="CFID"/>
</set>
</class>
</hibernate-mapping>
实体类Course.java
package com.tarena.enrollment.biz.entity;
import java.util.HashSet;
import java.util.Set;
public class Course {
private Long oid;
private String cid;
private String name;
private Set stus=new HashSet();
public Course(){}
public Course(String cid, String name) {
super();
this.cid = cid;
this.name = name;
}
public Course(String cid, String name, Set stus) {
super();
this.cid = cid;
this.name = name;
this.stus = stus;
}
public Long getOid() {
return oid;
}
public void setOid(Long oid) {
this.oid = oid;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getStus() {
return stus;
}
public void setStus(Set stus) {
this.stus = stus;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cid == null) ? 0 : cid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Course other = (Course) obj;
if (cid == null) {
if (other.cid != null)
return false;
} else if (!cid.equals(other.cid))
return false;
return true;
}
}
Student.java实体类
package com.tarena.enrollment.biz.entity;
import java.util.HashSet;
import java.util.Set;
public class Student {
private Long oid;
private String sid;
private String name;
private Set cours=new HashSet();
public Student(){}
public Student(String sid, String name) {
super();
this.sid = sid;
this.name = name;
}
public Student(String sid, String name, Set cours) {
super();
this.sid = sid;
this.name = name;
this.cours = cours;
}
public void registerCour(Course cour){
cours.add(cour);
cour.getStus().add(this);
}
public Long getOid() {
return oid;
}
public void setOid(Long oid) {
this.oid = oid;
}
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 getCours() {
return cours;
}
public void setCours(Set cours) {
this.cours = cours;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((sid == null) ? 0 : sid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Student other = (Student) obj;
if (sid == null) {
if (other.sid != null)
return false;
} else if (!sid.equals(other.sid))
return false;
return true;
}
}