Hibernate about using join tables to implement a one-to-many association mapping

Source: Internet
Author: User


"Hibernate" about using a join table to implement a one -to-many association mapping


A one-to-many association mapping based on an intermediate table is still more common.

Person (people) and group (group)


Annotations Configuration

@Entity @table (name= "T_group") publicclass group {    private integer id ;    private string name;    private set<person>  persons=newHashSet<Person> ();         @OneToMany       @JoinTable (name= "t_p", joincolumns={@JoinColumn (name= "g_id")},            inversejoincolumns={@JoinColumn (name= "p_id")})      Public set<person> getpersons ()  {       returnpersons;     }    publicvoid setpersons (Set<Person> persons)  {       this.persons = persons;    }      @Id      @GeneratedValue     public integer  getid () &NBSP;{&NBSP;&Nbsp;     returnid;    }    publicvoid  SetId (Integer id)  {       this.id = id;     }     @Column (name= "G_name")     public String  GetName ()  {       returnname;    }     publicvoid setname (String name)  {        this.name = name;    }}
@Entity @table (name= "P_person") publicclass person {    private integer  Id;    private string name;    private integer age ;     @Id      @GeneratedValue     public integer  getid ()  {       returnid;    }     publicvoid setid (Integer id)  {       this.id  = id;    }     @Column (name= "P_name")      public string getname ()  {       returnname;     }    publicvoid setname (String name)  {        this.name = name;    }     @Column ( Name= "P_age")   &nbsP; public integer getage ()  {       returnage;     }    publicvoid setage (integer age)  {        this.age = age;    }}


XML configuration

<?xml version= "1.0"? ><! doctype hibernate-mapping public      "-//hibernate/hibernatemapping  dtd 3.0//en "    " Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">< Hibernate-mapping package= "csg.hibernate.entity" >    <class name= "Group"  table= "T_group" >       <id name= "id" >            <column name= "id"/>            <generator class= "native"  />        </id>       <property name= "Name"  />        <set name= "Persons"  table= "T_p" >            <key column= "g_id"/>           <many-to-many column= "p_id"  class= "person"   Unique= "true"  />       </set>    </ Class>
<?xml version= "1.0"? ><! doctype hibernate-mapping public      "-//hibernate/hibernatemapping  dtd 3.0//en "    " Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">< Hibernate-mapping package= "csg.hibernate.entity" >    <class name= "person"  table= "T_person" >       <id name= "id" >            <column name= "id"/>            <generator class= "native"  />        </id>       <property name= "Name"  / >       <property name= "Age"  />     </class>


In a one-to-many intermediate table mapping, Hibernate automatically translates to many-to-many processing!


This article from "Promise always attached to the small wood 、、、" blog, please be sure to keep this source http://1936625305.blog.51cto.com/6410597/1569047

Hibernate about using join tables to implement a one-to-many association mapping

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.