Hibernate from Getting started to mastering (vi) One-to-one bidirectional Association mapping.

Source: Internet
Author: User
Tags generator
In the last blog post hibernate from Getting started to mastering (v.) one to one one-way association mapping we explained one to one one-way association mapping, this time we continue to explain the corresponding one-to-one two-way association mapping.


A one-to-one two-way association differs from a one-to-one one-way association mapping in a one-to-one two-way association where a reference to another object (Idcard) is present in one object (person), and the object (person) is also present in another object (Idcard) So that you can find another object (Idcard) while loading the object (person), or you can find the object (person) when you load another object (Idcard).

The class structure diagram looks like this: The specific code is as follows:[Java] View Plain copy print? public class person {       private int id;                 private String name;                     private idcard idcard;              public  int getid ()  {           return id;        }       public void setid (int id)  {           this.id = id;        }       public string getname ()  {           return name;       }       public void setname (string name)  {           this.name = name;        }       public idcard getidcard ()  {            return idCard;       }        public void setidcard (idcard idcard)  {            this.idCard = idCard;       }   }      public class idcard {               private int id;             private String cardNo;              private person person;              public  int getid ()  {           return id;        }       public void setid (int id)  {           this.id = id;        }       public string getcardno ()  {           return cardNo;       }        public void setcardno (String cardno)  {            this.cardNo = cardNo;       }        public person getperson ()  {            return person;       }        public void setperson (Person person)  {            this.person = person;       }  }   

		public class Person {
			private int id;			
			private String name;			
			Private Idcard Idcard;
			
			public int getId () {return
				ID;
			}
			public void setId (int id) {
				this.id = ID;
			}
			Public String GetName () {return
				name;
			}
			public void SetName (String name) {
				this.name = name;
			}
			Public Idcard Getidcard () {return
				idcard;
			}
			public void Setidcard (Idcard idcard) {
				this.idcard = Idcard;
			}
		}
		
		public class Idcard {		
			private int id;		
			Private String Cardno;		
			private person person;
			
			public int getId () {return
				ID;
			}
			public void setId (int id) {
				this.id = ID;
			}
			Public String Getcardno () {return
				cardno;
			}
			public void Setcardno (String cardno) {
				this.cardno = Cardno;
			}
			Public Person Getperson () {return person
				;
			}
			public void Setperson (person person) {
				This.person = person;
			}
		}

As with one-to-one one-way association mappings, a one-to-one, two-way association mapping is also associated with different policies that generate different table structures and can be divided into primary key associations and unique foreign key associations. But the same as a one-to-one one-way association mapping is the resulting table structure, that is, a one-to-one bidirectional association map, compared to a one-to-one one-way association, only changes the loading of one-to-one correlation mappings and does not change storage.

Primary Key AssociationThe Primary Key association table structure is as follows: a one-to-one one-way association mapping, a primary key association is associated with a primary key, and the value of the associated primary key is the same. The corresponding *.hbm.xml configuration file code is as follows:
[HTML] View Plain copy print? <class name= "Com.zs.hibernate.IdCard"  table= "T_idcard" >        <id name= "id" >           <generator class= "Native"/>       </id>       <property  name= "Cardno"/>       <one-to-one name= "person"/>   </class>      <class name= "Com.zs.hibernate.Person"  table= "T_person" >        <id name= "id" >            <generator class= "foreign" >                <param name= "Property" >idCard</param>            </generator>       </id>       <property name= "Name"/>                            <one-to-one name= "Idcard"  constrained= "true"/>   </class>  
			<class name= "Com.zs.hibernate.IdCard" table= "T_idcard" >
				<id name= "id" >
					<generator class= " Native "/>
				</id>
				<property name=" Cardno "/> <one-to-one name=
				" person "/>
			</ class>
			
			<class name= "Com.zs.hibernate.Person" table= "T_person" >
				<id name= "id" >
					< Generator class= "foreign" >
						<param name= "property" >idCard</param>
					</generator>
				</id>
				<property name= "name"/>					
				<one-to-one name= "Idcard" constrained= "true"/>
			</class>

Unique Foreign Key Association

The unique Foreign Key association table structure is as follows:

The Foreign Key association mappings for one-to-one bidirectional association mappings are also similar to the Foreign Key association mappings for one-to-one one-way association mappings, with a unique foreign key in its one-to-one pointing (person), which is associated with the pointed end (Idcard) and has the same value as the associated primary key. The corresponding *.hbm.xml configuration file code is as follows:
[HTML] View Plain copy print? <class name= "Com.zs.hibernate.IdCard"  table= "T_idcard" >        <id name= "id" >           <generator class= "Native"/>       </id>       <property  name= "Cardno"/>       <one-to-one name= "person"   property-ref= "Idcard"/>   </class>      <class name= " Com.zs.hibernate.Person " table=" T_person ">       <id name=" id " >           <generator class= "native"/>        </id>       <property name= "name"/ >       <many-to-one name= "Idcard"  unique= "true"/>   </class>  

			<class name= "Com.zs.hibernate.IdCard" table= "T_idcard" >
				<id name= "id" >
					<generator class= " Native "/>
				</id>
				<property name=" Cardno "/> <one-to-one name=
				" person "property-ref=" Idcard "/>
			</class>
			
			<class name=" Com.zs.hibernate.Person "table=" T_person ">
				<id Name = "id" >
					<generator class= "native"/>
				</id>
				<property name= "name"/>
				< Many-to-one name= "Idcard" unique= "true"/>
			</class>

You can also replace XML configuration mappings by using annotations in the class file.

So far, one-way and two-way one-to-one relationships have been explained. Not enough detail, just to give you a few examples and a few differences, but for the mastery and application of hibernate these can be. Next time we'll go on to explain some of the other hibernate 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.