One (' many ')
Import Javax.persistence.cascadetype;import Javax.persistence.entity;import javax.persistence.GeneratedValue; Import Javax.persistence.id;import Javax.persistence.onetoone;import org.hibernate.annotations.GenericGenerator; Import Org.hibernate.annotations.Parameter; @Entitypublic class Wife {private int id; private String name; Private Husband Husband; /** * Wife ID is assigned according to husband ID, the ID generator must be set to the foreign * * parameter specified wife ID is used in the husband object ID */@Id @GenericG Enerator (name = "Pkgenerator", strategy = "foreign", parameters = {@Parameter (name = "Property", value = "husband")}) @ Generatedvalue (generator = "pkgenerator") 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 void Sethusband (Husband Husband) {this.husband = Husband; } @OneToOne (cascade = Cascadetype.all) Public Husband Gethusband () {return Husband; }}
One (' one ')
Import Javax.persistence.cascadetype;import Javax.persistence.entity;import javax.persistence.GeneratedValue; Import Javax.persistence.id;import Javax.persistence.onetoone;import javax.persistence.PrimaryKeyJoinColumn; @Entitypublic class Husband { private int id; private String name; Private Wife Wife; @Id @GeneratedValue Public int getId () { return Id; } Public String GetName () { return name; } This annotation can only be written at one end of the main (build ID) @OneToOne (cascade = cascadetype.all) @PrimaryKeyJoinColumn 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;
Hibernate-----One-to-one shared primary key association mappings (annotations)