hibernate中多對多關係映射

來源:互聯網
上載者:User
案例:

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>
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.