Hibernate's about many-to-one one-way association mappings

Source: Internet
Author: User



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 )

Id Name
1 Tom
2 John doe

(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

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.