hibernate中多对多关系映射

案例:

person:一个人可以有担任很多项工作

job:一项工作可以由很多人担任

这在person和job中就形成了多对多的关系,映射成一张新表。

package com.pk.mapping;
import java.util.HashSet;
import java.util.Set;
public class Job {
private int id;
private String jobname;
private Set people=new HashSet();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getJobname() {
return jobname;
}
public void setJobname(String jobname) {
this.jobname = jobname;
}
public Set getPeople() {
return people;
}
public void setPeople(Set people) {
this.people = people;
}
}
package com.pk.mapping;
import java.util.HashSet;
import java.util.Set;
public class People {
private int id;
private String name;
private Set jobs=new HashSet();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getJobs() {
return jobs;
}
public void setJobs(Set jobs) {
this.jobs = jobs;
}
}
<?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.pk.mapping">
<class name="Job" table="t_job">
<id name="id" column="t_job_id">
<generator class="native"></generator>
</id>
<property name="jobname" column="t_job_name"></property>
<set name="people" table="t_job_people" >
<!--外键,必须以本表的id作为参照物-->
<key column="job_id"></key>
<many-to-many class="People" column="people_id" />
</set>
</class>
</hibernate-mapping>
<?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.pk.mapping">
<class name="People" table="t_people">
<id name="id" column="t_people_id">
<generator class="native"></generator>
</id>
<property name="name" column="t_people_name"></property>
<set name="jobs" table="t_job_people">
<key column="people_id"></key>
<many-to-many class="Job" column="job_id"></many-to-many>
</set>
</class>
</hibernate-mapping>

郑重声明:本文版权包含图片归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们(delete@yzlfxy.com)修改或删除,多谢。

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

留言与评论(共有 0 条评论)
昵称:
匿名发表
   
验证码: