Hibernate关联关系映射(一对一关联)

一对一关联是另外一种常见关系。如果一个A的实例最多关联一个B的实例,而一个B的实例也最多关联一个A 的实例,几倍称为双向的一对一关联关系。

A类和B类的演示代码如下:

public class A{
//A类其他属性
...
//A类关联一个B实例
private B b;
}
public class B{
//B类其他属性
...
//B类关联一个A实例
private A a;
}

基于主键的一对一关联

create table people(
	id int not null primary key,
	age int,
	name varchar(20)
);
create table passport(
	id int not null primary key,
	serial varchar(20),
	expiry int 
)
alter table passport add constraint foreign key(id) references people(id);
People类

package onetoone;

public class People {

	private Integer id;
	private Integer age;
	private String name;
	private Passport passport;
	public People() {
		super();
	}
	public People(Integer id, Integer age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Passport getPassport() {
		return passport;
	}
	public void setPassport(Passport passport) {
		this.passport = passport;
	}
	
}
Passport类

package onetoone;

public class Passport {

	private Integer id;
	private People people;
	private String serial;
	private Integer expiry;
	
	public Passport() {
		// TODO Auto-generated constructor stub
	}

	public Passport(People people) {
		super();
		this.people = people;
	}

	public Passport(String serial, Integer expiry) {
		super();
		this.serial = serial;
		this.expiry = expiry;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public People getPeople() {
		return people;
	}

	public void setPeople(People people) {
		this.people = people;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public Integer getExpiry() {
		return expiry;
	}

	public void setExpiry(Integer expiry) {
		this.expiry = expiry;
	}

}
People.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>
<class name="onetoone.People" table ="people" catalog="mldn">
	<id name="id" type ="java.lang.Integer">
		<column name="id"/>
		<generator class ="assigned"/>
	</id>
	<property name="age" type ="java.lang.Integer">
		<column name="age" />
	</property>
		<property name="name" type ="java.lang.String">
		<column name="name" length="20" />
	</property>
		<one-to-one name="passport" cascade="all">
		</one-to-one>
</class>
</hibernate-mapping>

Passport.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>
<class name="onetoone.Passport" table ="passport" catalog="mldn">
	<id name="id" type ="java.lang.Integer">
		<column name="id"/>
		<generator class ="foreign">
			<param name="property">people</param>
		</generator>
	</id>
	<one-to-one name="people" constrained="true"></one-to-one>
	<property name="serial" type ="java.lang.String">
		<column name="serial" length="20" />
	</property>
	<property name="expiry" type ="java.lang.Integer">
		<column name="expiry" />
	</property>
</class>
</hibernate-mapping>

测试类

public static void main(String[] args) {
		People people = new People(1,20,"Jamas");
		Passport passport = new Passport("10099",6);
		people.setPassport(passport);
		passport.setPeople(people);
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = session.beginTransaction();
		session.save(people);
		tran.commit();
	}

运行结果:



基于唯一外键的一对一关联

创建表:

create table people(
id int not null primary key,
age int,
name varchar(20)
)
;

create table passport(
id int not null primary key,
serial varchar(20),
expiry int,
people_id int unique,
foreign key(people_id) references People(id)
)
修改People.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>
<class name="onetoone.People" table ="people" catalog="mldn">
	<id name="id" type ="java.lang.Integer">
		<column name="id"/>
		<generator class ="assigned"/>
	</id>
	<property name="age" type ="java.lang.Integer">
		<column name="age" />
	</property>
		<property name="name" type ="java.lang.String">
		<column name="name" length="20" />
	</property>
		<one-to-one name="passport" cascade="all" property-ref="people">
		</one-to-one>
</class>
</hibernate-mapping>

修改Passport.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>
<class name="onetoone.Passport" table ="passport" catalog="mldn">
	<id name="id" type ="java.lang.Integer">
		<column name="id"/>
		<generator class ="assigned"/>
	</id>
	<many-to-one name="people" >
		<column name="people_id" unique="true"></column>
	</many-to-one>
	<property name="serial" type ="java.lang.String">
		<column name="serial" length="20" />
	</property>
	<property name="expiry" type ="java.lang.Integer">
		<column name="expiry" />
	</property>
</class>
</hibernate-mapping>
测试类:

	public static void main(String[] args) {
		People people = new People(1,20,"Jamas");
		Passport passport = new Passport("10099",6);
		passport.setId(21);
		people.setPassport(passport);
		passport.setPeople(people);
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = session.beginTransaction();
		session.save(people);
		tran.commit();
	}

显示结果:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值