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 () {       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") 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 ">


<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public     "-//hibernate/hibernatemapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

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




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.