hibernate关联映射

学生类代码

package cn.bdqn.entity;

import java.util.HashSet;
import java.util.Set;

public class Student {
	private int sid;
	private String sname;
	private String sex;
	private Grade grade;
	private Paper paper;
	private Set<Course> course = new HashSet<Course>();

	public Set<Course> getCourse() {
		return course;
	}

	public void setCourse(Set<Course> course) {
		this.course = course;
	}

	public Paper getPaper() {
		return paper;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Grade getGrade() {
		return grade;
	}

	public void setGrade(Grade grade) {
		this.grade = grade;
	}

	public Student(int sid, String sname, String sex) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.sex = sex;
	}

	public Student() {
		super();
	}

}
班级类代码
package cn.bdqn.entity;

import java.util.HashSet;
import java.util.Set;

public class Grade {
	private int gid;
	private String gname;
	private String gdesc;
	private Set<Student> students = new HashSet<Student>();

	public int getGid() {
		return gid;
	}

	public void setGid(int gid) {
		this.gid = gid;
	}

	public String getGname() {
		return gname;
	}

	public void setGname(String gname) {
		this.gname = gname;
	}

	public String getGdesc() {
		return gdesc;
	}

	public void setGdesc(String gdesc) {
		this.gdesc = gdesc;
	}

	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

}

下面的红色字体是关键代码

1.单向一对多关联关系

映射文件关键代码

Grade.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="cn.bdqn.entity">
    <class name="Grade" table="grade">
       <id name="gid" column="gid" type="java.lang.Integer">
           <generator class="assigned"/>
       </id>
       <property name="gname" type="java.lang.String" column="gname"/>
       <property name="gdesc" type="java.lang.String" column="gdesc"/>
      <span style="color:#ff0000;"> <set name="students"  cascade="save-update" inverse="true">
            <key column="gid"/>
            <one-to-many class="Student"/>
        </set></span>
    </class>
</hibernate-mapping>

2.单向多对一关联关系

映射文件关键代码

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="cn.bdqn.entity">

	<class name="Student" table="pb_student">
		<id name="sid" column="sid" type="java.lang.Integer">
			<generator class="assigned" />
		</id>
		<property name="sname" type="java.lang.String" column="sname" />
		<property name="sex" type="java.lang.String" column="sex" />
		<span style="color:#ff0000;"><many-to-one name="grade" class="Grade" cascade="save-update">
			<column name="gid"></column>
		</many-to-one></span>
	</class>
</hibernate-mapping>

3.一对一关联关系

映射文件关键代码

Student.hbm.xml中添加如下代码

<span style="font-size:12px;"><span style="color:#ff0000;"><one-to-one name="paper" class="Paper" cascade="all" lazy="false" </span><span style="color: rgb(255, 0, 0); font-family: Arial, Helvetica, sans-serif;">property-ref="student"></one-to-one></span></span>
学生证类

package cn.bdqn.entity;

public class Paper {
	private int pid;
	private String pdesc;
	private Student student;
	public Paper() {
		super();
	}

	public Paper(int pid, String pdesc) {
		super();
		this.pid = pid;
		this.pdesc = pdesc;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getPdesc() {
		return pdesc;
	}

	public void setPdesc(String pdesc) {
		this.pdesc = pdesc;
	}

}
映射文件Paper.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="cn.bdqn.entity">
    <class name="Paper" table="paper">
        <id name="pid" column="pid" type="java.lang.Integer">
            <generator class="assigned"/>
        </id>
        <property name="pdesc" type="java.lang.String" column="pdesc"/>
        <span style="color:#ff0000;"><many-to-one name="student" class="Student"  unique="true" lazy="false">
        	<column name="sid"></column>
        </many-to-one> </span>   
    </class>
</hibernate-mapping>

4.多对多关联关系

映射文件关键代码

Student.hbm.xml中添加如下代码

<span style="color:#ff0000;"><set name="course" table="sc" cascade="save-update"> <key column="sid"></key> 
			<many-to-many class="Course" column="cid"></many-to-many> 
			</set></span>

课程表类

package cn.bdqn.entity;

import java.util.HashSet;
import java.util.Set;

public class Course {
	private int cid;
	private String cname;
	private String cdesc;
	private Set<Student> student = new HashSet<Student>();
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public String getCdesc() {
		return cdesc;
	}
	public void setCdesc(String cdesc) {
		this.cdesc = cdesc;
	}
	public Set<Student> getStudent() {
		return student;
	}
	public void setStudent(Set<Student> student) {
		this.student = student;
	}

}
映射文件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="cn.bdqn.entity">
    <class name="Course" table="course">
        <id name="cid" column="CID" type="java.lang.Integer">
            <generator class="assigned"/>
        </id>
        <property name="cname" type="java.lang.String" column="CNAME"/>
        <property name="cdesc" type="java.lang.String" column="CDESC"/>
        <!-- 和学生为多对多映射 -->
       <span style="color:#ff0000;"> <set name="student" table="sc" cascade="save-update" inverse="true">
        	<key column="cid"></key>
        	<many-to-many class="Student" column="sid"></many-to-many>
        </set></span>
    </class>
</hibernate-mapping>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值