实体xml文件映射多对多
2010-09-18 14:33
302 查看
Student实体
Subject实体
中间表Grade
主键类
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;
}
}
<?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;
}
}
相关文章推荐
- applicationContext.xml文件加入 实体与表的映射文件(.hbm.xml),报错Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
- 通过mybatis工具generatorConfig.xml自动生成实体,DAO,映射文件
- 使用JPA Annotation标注实体---5.8.2: Annotation还是XML映射文件?
- 通过mybatis工具generatorConfig.xml自动生成实体,DAO,映射文件
- NHibernate实体映射文件生成模版(MyGeneration)
- hibernate映射文件详解:xxx.hbm.xml文件详解
- Mapper的XML映射文件---2.1: 属性(properties)
- Mapper的XML映射文件---2.8: 映射器(Mappers)
- Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
- 实体与表映射关系XXX.hbm.xml配置详解(转)
- mybatis generatorConfig.xml如何执行生成实体类和映射文件
- NHibernate映射关系表(实体映射文件字段的属性对应的NHibernate类型)
- 详解hibernate实体映射文件
- hibernate 的 使用 MyEclipse 工具生成 POJO 类和映射文件*.hbm.xml
- myEclipse通过表生成hibernate实体和映射文件
- 对xsd文件的操作 生成java实体并解析生成的xml
- hibernate映射文件??.hbm.xml配置映射元素详解--Hibernate映射类型
- hibernate实体配置文件 *.hbm.xml
- PowerDesigner XML 文件直接生成 VB 实体(包含属性说明)、ibatisNet 代码
- MyBatis官方文档——XML 映射配置文件