A multi-pair one-way Association mapping for "Hibernate"
In the development of the project many-to-one one-way association mapping is the most common, association mapping! This is an emphasis on detailed explanation!
For example, we now have a group and people (person)
(Persontable )
(Group table )
Id |
Name |
p_id |
1
|
Finance Group |
1 |
2 |
Finance Group |
2 |
From the above table we find that there is no duplication of data? The Finance group is repeating!
So, in many-to-one one-way mappings, we usually associate fewer parties with a foreign key on many sides.
So in this relationship (group) is a small party, (person) is a multi-party, a group can have more than one!
(Group table )
Id |
Name |
1 |
Finance Group |
2 |
Development Group |
(Persontable )
Id |
Name |
g_id |
1
|
Tom |
1 |
2 |
John doe |
1
|
The above relationship is the normal business logic!
ok, let's look at our annotations Configuration!
@Entity @table (name= "T_user") publicclass person { private integer id ; private string name; private integer age; private group group; @ManyToOne public group getgroup () { returngroup; } publicvoid setgroup ( Groupgroup) { this.group = group; } @Id @GeneratedValue public Integer getid () { returnid; } publicvoid setid (Integerid) { this.id = id; } @Column (name= "name") public string getname () { returnname; } Publicvoid setname (Stringname) { this.name = name ; } @Column (name= "age") public Integer getage () { returnage; } publicvoid setage (integerage) { this.age = age; }}
@Entity @table (name= "T_group") publicclass Group {private Integer ID; private String name; @Id @GeneratedValue public Integer getId () {Returnid; } publicvoid setId (integerid) {this.id = ID; } @Column (name= "name") public String GetName () {returnname; } publicvoid SetName (stringname) {this.name = name; }}
XML Configuration Methods
package csg.hibernate.entity; publicclass person { private integer id; private string name; private integer age; private group group; public group getgroup () { returngroup; } publicvoid setgroup (Groupgroup) { this.group = group; } public integer getid () { returnid; } publicvoid setid (Integerid) { This.id = id; } public string getname () { returnname; } publicvoid setname (Stringname) { this.name = name; } public integer getage () { returnage; } publicvoid setage (integerage) { this.age = age; }}
package csg.hibernate.entity; publicclass group { private Integer id; private String name; public integer getid () { returnid; } publicvoid setid ( Integerid) { this.id = id; } public string getname () { Returnname; } publicvoid setname (Stringname) { this.name = name; }}
<?xml version= "1.0"? ><! doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en " " HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-3.0.DTD " >
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net/hib Ernate-mapping-3.0.dtd ">
Here we use JUnit to test
publicclass junitest {privatestatic sessionfactory sessionfactory; @BeforeClass publicstaticvoid beforeclass () { configuration cfg=new configuration (); cfg.configure (); sessionfactory= Cfg.buildsessionfactory (); } @Test public void add () { try { group group=new group (); Session session = Sessionfactory.getcurrentsession (); Session.begintransaction (); group.setname ("department"); Session.save (group); person person=new person (); person.setage (; ) person.setname ("Zhang San"); person.setgroup (group); session.save (person); session.gettransaction (). Commit (); } catch ( Hibernateexception e) { e.printstacktrace (); } } @AfterClass publicstaticvoid afterclass () { sessionfactory.close (); }}
Ok!
This article from "Promise always attached to the small wood 、、、" blog, please be sure to keep this source http://1936625305.blog.51cto.com/6410597/1568855
Hibernate's about many-to-one one-way association mappings