Husband.java
package com.one2one.bean;
public class Husband {
private int id;
private String name;
private Wife wife;
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 Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife.java
package com.one2one.bean;
public class Wife {
private int id;
private String name;
private Husband husband;
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 Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}
映射文件
Husband.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.one2one.bean">
<class name="Husband" table="husband" catalog="Hibernate_One2One_fk">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<one-to-one name="wife" class="Wife" property-ref="husband"/>
</class>
</hibernate-mapping>
<one-to-one name="wife" class="Wife" property-ref="husband"/> property-ref后的husband为Wife中定义的husband属性
Wife.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.one2one.bean">
<class name="Wife" table="wife" catalog="Hibernate_One2One_fk">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<many-to-one name="husband" column="husband_id" unique="true" />
</class>
</hibernate-mapping>
<many-to-one name="husband" column="husband_id" unique="true" />
many-to-one的作用是在wife表中生成一个外键husband_id,且唯一,并参照husband表中的主键
用hibernate的hbm2ddl导出的sql
create table Hibernate_One2One_fk.husband (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table Hibernate_One2One_fk.wife (
id integer not null auto_increment,
name varchar(255),
husband_id integer unique,
primary key (id)
)
alter table Hibernate_One2One_fk.wife
add index FK37AF119039AEDD (husband_id),
add constraint FK37AF119039AEDD
foreign key (husband_id)
references Hibernate_One2One_fk.husband (id)
测试示例
@Test
public void save(){
Session session=HibernateSessionFactory.getSession();
Transaction transaction=session.beginTransaction();
try {
transaction.begin();
Husband husband=new Husband();
husband.setName("张三");
session.save(husband);
Wife wife=new Wife();
wife.setName("如花");
wife.setHusband(husband);
session.save(wife);
transaction.commit();
} catch (HibernateException e) {
e.printStackTrace();
}
}
@Test
public void select(){
Session session=HibernateSessionFactory.getSession();
Husband husband=(Husband) session.get(Husband.class, 1);
System.out.println(husband.getName());
System.out.println(husband.getWife().getName());
}