"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