"Hibernate" About one-way, bidirectional Association mappings
First we look at Hibernate's official explanation for one-to-one unidirectional association:
The one-way and one-way associations based on the foreign Key association are almost the same as the unidirectional many-to-one association. The only difference is a one-way off.
The Foreign key field in the union has a uniqueness constraint.
OK, then we can also understand that one-to-many is a special form of association, we know that hibernate is configured in two ways, namely, Annotations,XML two configuration mode!
Annotations One-way Association mappings
Person (User) and Social Security Number (card)
@Entity @table (name= "T_user") publicclass user { private Integer ID; private String name; Private card card; @OneToOne @JoinColumn (name= "card_id", Unique=true)//name is the column name of the Custom Association foreign key public card Getcard () { Returncard ; } Publicvoid Setcard (card) { this.card = card; } @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; }
@Entity @table (name= "T_card") Publicclass card { private Integer ID; Private String num; @Id @GeneratedValue public Integer getId () { returnid; } Publicvoid setId (integerid) { this.id = ID; } @Column (name= "card_id") public String Getnum () { returnnum; } Publicvoid Setnum (stringnum) { this.num = num; }}
XML One-Way Association
Person and the Group , a person can only be in a group
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; }}
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 () { return name; } public void SetName (Stringname) { this.name = name; } Public Integer Getage () { return age; } Publicvoid setage (Integer age) { this.age = age; }}
<?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 ">
Ok, here, everyone is a little puzzled, why our annotaions configuration is one-to-one and our XML configuration is Many-to-one?
Of course, the XML configuration is my reference to hibernate's documentation, because one on the other is a multi-pair special form,
But configuration Many-to-one gives us a hint that multiple numbers correspond to one person, so hibernate provides unique uniqueness to confirm this situation!
Annotations one-to-one bidirectional correlation mapping
User and the Card
@Entity @table (name= "T_user") publicclass user { private Integer ID; private String name; Private card card; @OneToOne @JoinColumn (name= "card_id", Unique=true)//name is primarily a custom association foreign key column name public card Getcard () { Returncard; } Publicvoid Setcard (cardcard) { this.card = card; } @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; }}
@Entity @table (name= "T_card") Publicclass card { private Integer ID; Private String num; private user user; @OneToOne (mappedby= "card")//mappedby means to specify the card in user as the associated foreign key, otherwise the user and card will appear the foreign key public user GetUser () { Returnuser; } Publicvoid SetUser (useruser) { this.user = user; } @Id @GeneratedValue public Integer getId () { returnid; } Publicvoid setId (integerid) { this.id = ID; } @Column (name= "card_id") public String Getnum () { returnnum; } Publicvoid Setnum (stringnum) { this.num = num; }}
Ok? So we can see that the configuration in Annotaions is Ont-to-one, so what are our configurations in XML? <?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/hibernate-mapping-3.0.dtd ">
OK, to here a one-way correlation mapping is basically the case, essays, there are problems, please leave a message!
Hibernate about one-way, bidirectional Association mappings