Hibernate!! Multi-to-many (unidirectional | bidirectional) Association Mappings

Source: Internet
Author: User

Many-to-many-unidirectional association mappings
1  PackageCom.hb.model;2 3 ImportJava.util.HashSet;4 ImportJava.util.Set;5 6 Importjavax.persistence.Entity;7 ImportJavax.persistence.GeneratedValue;8 Importjavax.persistence.Id;9 ImportJavax.persistence.ManyToMany;Ten Importjavax.persistence.Table; One  A @Entity -@Table (name= "T_teacher") -  Public classTeacher { the     Private  intTid; -     PrivateString Tname; -     Privateset<student> students =NewHashset<student>(); -      + @Id - @GeneratedValue +      Public intGettid () { A         returnTid; at     } -      Public voidSettid (inttid) { -          This. Tid =Tid; -     } -      PublicString Gettname () { -         returnTname; in     } -      Public voidsettname (String tname) { to          This. Tname =Tname; +     } - @ManyToMany the      PublicSet<student>getstudents () { *         returnstudents; $     }Panax Notoginseng      Public voidSetstudents (set<student>students) { -          This. Students =students; the     } +      A      the  +}
 PackageCom.hb.model;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;Importjavax.persistence.Table; @Entity @table (name= "T_student") Public classStudent {Private  intSID; PrivateString sname; @Id @GeneratedValue Public intGetSID () {returnSID; }     Public voidSetsid (intSID) {         This. Sid =SID; }     PublicString Getsname () {returnsname; }     Public voidsetsname (String sname) { This. sname =sname; }        }

The resulting table is as follows:

The name of the resulting intermediate table is automatically generated, or you can change the name of the table by adding some properties

Just add some attributes to the teacher class, and the modified code is as follows:

1 @Entity2@Table (name= "T_teacher")3  Public classTeacher {4     Private  intTid;5     PrivateString Tname;6     Privateset<student> students =NewHashset<student>();7     8 @Id9 @GeneratedValueTen      Public intGettid () { One         returnTid; A     } -      Public voidSettid (inttid) { -          This. Tid =Tid; the     } -      PublicString Gettname () { -         returnTname; -     } +      Public voidsettname (String tname) { -          This. Tname =Tname; +     } A @ManyToMany at@JoinTable (name= "t_s",//Table name -joincolumns={@JoinColumn (name= "teacher_id")},//???  -inversejoincolumns={@JoinColumn (name= "student_id")}//The ID of the corresponding foreign key for that table. -         ) -      PublicSet<student>getstudents () { -         returnstudents; in     } -      Public voidSetstudents (set<student>students) { to          This. Students =students; +     } -}

Configuration of a many-to-many one-way association in an XML file

1 <hibernate-mapping>2     <classname= "Com.hb.model.Teacher">3         <IDname= "Tid">4             <Generatorclass= "Native"></Generator>5         </ID>6         7         < Propertyname= "Tname"></ Property>8         <Setname= "Students"Table= "t_s">9             <Keycolumn= "teacher_id"></Key>Ten             <Many-to-manyclass= "com.hb.model. Student "column= "student_id"/> One         </Set> A     </class> -      - </hibernate-mapping>
Many-to-many-bidirectional association mappings
    • Rarely used
    • The Mappedby is always used in two directions.

Hibernate!! Multi-to-many (unidirectional | bidirectional) Association Mappings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.