Hibernate Annotation @mappedBy meaning when using onetoone "Foreign Key Bidirectional Association" (13)

Source: Internet
Author: User

1

Package com.bjsxt.hibernate;

Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import Javax.persistence.JoinColumn;
Import Javax.persistence.OneToOne;

@Entity public
class husband {
	private int id;
	private String name;
	Private Wife Wife;
	@Id
	@GeneratedValue Public
	int getId () {return
		Id;
	}
	
	Public String GetName () {return
		name;
	}
	@OneToOne
	@JoinColumn (name= "Wifeid") public
	Wife Getwife () {return
		Wife;
	}
	public void setId (int id) {
		this.id = ID;
	}
	public void SetName (String name) {
		this.name = name;
	}
	public void Setwife (Wife Wife) {
		this.wife = Wife;
	}
	
}

Package com.bjsxt.hibernate;


Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import Javax.persistence.OneToOne;


@Entity
public class Wife {
private int id;
private String name;
Private husband husband;
@OneToOne (mappedby= "wife")
Public husband Gethusband () {
return husband;
}
public void Sethusband (husband husband) {
This.husband = husband;
}
@Id
@GeneratedValue
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;
}

}

Through bidirectional Association, the concrete Mapperby look below:

the @OneToOne here (mappedby= "wife") wife refers to Hudson's variable wife, which cannot be written casually.

1, on the meaning of Mappedby : a) only onetoone,onetomany,manytomany on the Mappedby attribute, Manytoone does not exist; b) Mappedby label must be defined in the     Owned side (by the owner), he points to the owning side (owner); c) Mappedby meaning, it should be understood that the owner can automatically maintain the relationship with the owner, of course, if from the owner, by hand force to maintain the owner's relationship can be done. For example:

Person.java @OneToOne (optional = true, Cascade = cascadetype.all, Mappedby = "Person")
Public Idcard Getidcard () {
return idcard;
} Idcard.java @OneToOne (optional = false, Cascade = Cascadetype.all)
@JoinColumn (name = "person_id", referencedcolumnname = "PersonID", unique= true)
Public Person Getperson () {
return person;
Onetoonedaobean.java/* The above example Idcard for the owner, where the person is forcibly used as the main control, the relationship of two entities is maintained by hand/public void InsertPerson (String name, Boole An sex,short age, Date birthday,string cardid) {
person who = new person ();
Person.setname (name);
Person.setsex (Sex);
Person.setage (short.valueof (age));
Person.setbirthday (birthday);
Idcard Idcard = new Idcard (cardid);
Idcard.setperson (person);
Person.setidcard (Idcard);
Em.persist (person);
}/* In fact, since Idcard is the owner, he automatically maintains the relationship of two entities/public void InsertPerson1 (String name, Boolean sex,short age, Da Te birthday,string cardid) {
person who = new person ();
Person.setname (name);
Person.setsex (Sex);
Person.setage (short.valueof (age));
Person.setbirthday (birthday);
Idcard Idcard = new Idcard (cardid);
Idcard.setperson (person);
Person.setidcard (Idcard);
Em.persist (person);
Em.persist (Idcard);
}
D Mappedby and joincolumn/jointable are always mutually exclusive parties, it can be understood that because the owner of the association by the owner of the field exists, have just owned by the owner. Mappedby This definition of joincolumn/jointable is always invalid, does not establish the corresponding field or table; e) standard relationship mapping, defined in Ejb-3_0-fr-spec-persistence.pdf 2.1.8 Relationship Mapping Defaults Chapter:
The following rules apply to bidirectional relationships:
the inverse side of a bidirectional relationship must refer to it owning side by use of the
Mappedby element of the Onetoone, Onetomany, or manytomany annotation. The
Mappedby element designates the "property" or "field in the entity" is the owner of the relationship.
the many side of one-to-many/many-to-one bidirectional relationships The must be the owning
side, hence the mappedby element cannot is specified on the Manytoone annotation.
for one-to-one bidirectional relationships, the owning side, corresponds to the side
The corresponding foreign key.
The either side for many-to-many bidirectional relationships owning side.



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.