A one-to-one relationship between hibernate

Source: Internet
Author: User
Tags generator

There are two types of maintenance for one-to-one relationships, which are divided into primary key associations and unique foreign key associations

There are two types of maintenance for one-to-one relationships, which are divided into primary key associations and unique foreign key associations

Primary KEY Association: (bidirectional)

(main control Party)


Person.java


Person {


private String username;


//reference to the accused party


private address address;


public String GetUserName () {


return username;


}


public void Setusername (String username) {


this.username = Username;


}


public Address getaddress () {


return address;


}


public void setaddress (address) {


this.address= address;


}


}


(passive side)


Address.java


address{


private String Addressname;


//References to the main control


private person;


public String Getaddressname () {


return addressname;


}


public void Setaddressname (String addressname) {


this.addressname= Addressname;


}


Public Person Getperson () {


return person;


}


public void Setperson (person person) {


this.person= person;


}


public String toString () {


return to New Tostringbuilder (this).


Append ("Addressname", Addressname).


toString ();


}


public boolean equals (Object o) {


if (!) ( o instanceof address) {


return false;


}


Address RHS = (address) O;


return new Equalsbuilder ()


. Appendsuper (Super.equals (o))


. Append (This.addressname, Rhs.getaddressname ())


. Isequals ();


}


public int hashcode () {


return new Hashcodebuilder (17, 37).


Append (addressname).


Tohashcode ();


}


}


Mapping File


<class name= "person" table= "person" >


<id name= "id" type= "long" column= "id" >


<generator class= "native"/>


</id>


<property name= "username" column= "username" type= "string"/>


<one-to-one


name= "Address"


class= "Address"


cascade= "All"


constrained= "false"


/>


</class>


<class name= "Address" table= "Address" >


<id name= "id" column= "id" >


<generator class= "foreign" >


<param name= "property" >person</param>


</generator>


</id>


<one-to-one name= ' person ' class= ' person ' constrained= ' true '/>


<property name= "Addressname" column= "Addressname" type= "string"/>


</class>

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.