您的位置:首页 > 其它

实体xml文件映射多对多

2010-09-18 14:33 302 查看
Student实体

<?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="com.yxfei.dojo.Student" table="student">
<id column="id" name="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="stuName" column="stu_name" length="20" type="java.lang.String"></property>
<property name="sex" column="stu_sex" length="4" type="java.lang.String"></property>
<property name="age" column="stu_age" type="java.lang.Integer"></property>
<bag name="grade" cascade="none">
<key column="stu_id"></key>
<one-to-many class="com.yxfei.dojo.Grade"/>
</bag>
</class>
</hibernate-mapping>


Subject实体

<?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="com.yxfei.dojo.Subject" table="subject">
<id column="id" name="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="subName" column="sub_name" length="20" type="java.lang.String"></property>
<bag name="grade" cascade="all-delete-orphan">
<key column="sub_id"></key>
<one-to-many class="com.yxfei.dojo.Grade"/>
</bag>
</class>
</hibernate-mapping>


中间表Grade

<?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="com.yxfei.dojo.Grade" table="student_subject">
<composite-id name="gpk" class="com.yxfei.dojo.GradePK">
<key-many-to-one name="subject" column="sub_id" class="com.yxfei.dojo.Subject">
</key-many-to-one>
<key-many-to-one name="student" column="stu_id" class="com.yxfei.dojo.Student">
</key-many-to-one>
</composite-id>
<property name="mark" column="mark" type="java.lang.Integer"></property>
</class>
</hibernate-mapping>


主键类

package com.yxfei.dojo;

import java.io.Serializable;

public class GradePK implements Serializable {

/**
*
*/
private static final long serialVersionUID = -5279921322268472152L;
private Student student;
private Subject subject;

public GradePK()
{
}
public GradePK(Student student, Subject subject) {
this.student = student;
this.subject = subject;
}

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}
public Subject getSubject() {
return subject;
}

public void setSubject(Subject subject) {
this.subject = subject;
}

@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final GradePK other = (GradePK) obj;
if (student == null) {
if (other.student != null)
return false;
} else if (!student.equals(other.student))
return false;
if (subject == null) {
if (other.subject != null)
return false;
} else if (!subject.equals(other.subject))
return false;
return true;

}

@Override
public int hashCode() {
// TODO Auto-generated method stub
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((student == null) ? 0 : student.hashCode());
result = PRIME * result + ((subject == null) ? 0 : subject.hashCode());
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: