设置一对一双向外键关联关系,通过Husband可以找到Wife,也能通过Wife找到Husband
1、编写Husband类,并在Husband中添加Wife的引用 ;
使用Annotation时,将实体类进行注解,@Entity 、@Id;同时添加注解,设置关联关系为@OneToOne ;
- package com.hibernate._0700_one2one_bi_fk;
- 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 Integer id;
- private String name;
- private Wife wife; //去参考wife的id,此时wife必须先id
- @Id
- @GeneratedValue
- public Integer getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- @OneToOne //设置关联关系
- @JoinColumn(name="wifeId") //指定数据库中的名字,默认为wife_id
- public Wife getWife() {
- return wife;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setWife(Wife wife) {
- this.wife = wife;
- }
- }
package com.hibernate._0700_one2one_bi_fk;
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 Integer id;
private String name;
private Wife wife; //去参考wife的id,此时wife必须先id
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public String getName() {
return name;
}
@OneToOne //设置关联关系
@JoinColumn(name="wifeId") //指定数据库中的名字,默认为wife_id
public Wife getWife() {
return wife;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
2、编写实体类Wife,并在Wife中引用Husband;
- package com.hibernate._0700_one2one_bi_fk;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.OneToOne;
- @Entity
- public class Wife {
- private Integer id;
- private String name;
- private Husband husband;
- /** mappedBy="wife"中的这个wife是Husband中的wife属性,表示Husband起主导作用
- 如果不加,在数据库中Wife表中会生成husband_id这个冗余字段*/
- @OneToOne(mappedBy="wife")
- public Husband getHusband() {
- return husband;
- }
- @Id
- public Integer getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public void setHusband(Husband husband) {
- this.husband = husband;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
package com.hibernate._0700_one2one_bi_fk;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Wife {
private Integer id;
private String name;
private Husband husband;
/** mappedBy="wife"中的这个wife是Husband中的wife属性,表示Husband起主导作用
如果不加,在数据库中Wife表中会生成husband_id这个冗余字段*/
@OneToOne(mappedBy="wife")
public Husband getHusband() {
return husband;
}
@Id
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
使用Annotation时,将实体类进行注解,@Entity 、@Id;
同时也设置设置关联关系为@OneToOne,但是此时要加上属性mappedBy属性,
mappedBy="wife"中的这个wife是Husband中的wife属性,表示Husband起主导作用。
如果不加,在数据库中Wife表中会生成husband_id这个冗余字段;
3、编写测试类,生成数据库表,查看表的关系;
4、在xml文件中,在class标签中设置关联关系。
在Husband.hbm.xml 配置文件中介入
<many-to-one name="wife" column="wifeId" unique="true"></many-to-one>
unique="ture" 约束成一对一关联;
在Wife.hbm.xml 中添加
<one-to-one name="husband" property-ref="wife"></one-to-one>
property-ref指的是与之关联的类Husband中的属性wife。
5、总结: