Hibernate!!多對多(單向|雙向)關聯映射

來源:互聯網
上載者:User

標籤:des   style   blog   ar   io   color   os   sp   for   

多對多——單向關聯映射
 1 package com.hb.model; 2  3 import java.util.HashSet; 4 import java.util.Set; 5  6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.ManyToMany;10 import javax.persistence.Table;11 12 @Entity13 @Table(name="t_teacher")14 public class Teacher {15     private  int  tid ;16     private  String  tname;17     private  Set<Student>  students = new  HashSet<Student>();18     19     @Id20     @GeneratedValue21     public int getTid() {22         return tid;23     }24     public void setTid(int tid) {25         this.tid = tid;26     }27     public String getTname() {28         return tname;29     }30     public void setTname(String tname) {31         this.tname = tname;32     }33     @ManyToMany34     public Set<Student> getStudents() {35         return students;36     }37     public void setStudents(Set<Student> students) {38         this.students = students;39     }40     41     42 43 }
package com.hb.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="t_student")public class Student {    private  int sid;     private  String   sname;        @Id    @GeneratedValue    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;    }        }

產生的表如下:

 

這樣產生的中間表的名稱是自動產生的,也可以通過加入一些屬性改變表的名稱

只需要在Teacher類裡面添加一些屬性,修改後的代碼如下:

 1 @Entity 2 @Table(name="t_teacher") 3 public class Teacher { 4     private  int  tid ; 5     private  String  tname; 6     private  Set<Student>  students = new  HashSet<Student>(); 7      8     @Id 9     @GeneratedValue10     public int getTid() {11         return tid;12     }13     public void setTid(int tid) {14         this.tid = tid;15     }16     public String getTname() {17         return tname;18     }19     public void setTname(String tname) {20         this.tname = tname;21     }22     @ManyToMany23     @JoinTable(name="t_s",  //表名24             joinColumns={ @JoinColumn(name="teacher_id")},  //  ???25             inverseJoinColumns={@JoinColumn(name="student_id")}  // 對方那張表的對應的外鍵的id 26         )27     public Set<Student> getStudents() {28         return students;29     }30     public void setStudents(Set<Student> students) {31         this.students = students;32     }33 }

 

 

多對多單向關聯在xml檔案中的配置

 1 <hibernate-mapping> 2     <class name="com.hb.model.Teacher"> 3         <id name="tid"> 4             <generator class="native"></generator> 5         </id> 6          7         <property name="tname"></property> 8         <set name="students" table="t_s"> 9             <key column="teacher_id"></key>10             <many-to-many class="com.hb.model.Student" column="student_id"/>11         </set>12     </class>13     14 </hibernate-mapping>
多對多——雙向關聯映射
  • 很少用到
  • 雙向總會用到mappedBy

 

Hibernate!!多對多(單向|雙向)關聯映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.