Hibernate关联关系映射(一对一关联)
来源:互联网 发布:哪种编程语言最简单 编辑:程序博客网 时间:2024/05/16 11:02
一对一关联是另外一种常见关系。如果一个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
- Hibernate关联关系映射(一对一关联)
- Hibernate 映射一对一关联关系
- 【Hibernate】--关联关系映射:一对一关联
- hibernate映射一对一关联(主键关联)
- Hibernate关联关系映射之一对一
- Hibernate中的关联关系一对一映射
- Hibernate映射一对一的关联关系
- Hibernate关联关系映射之一对一关联关系
- Hibernate -- 映射实体关联关系(一对一关联关系)
- 一对一关联关系映射
- 映射一对一关联关系
- Hibernate关联映射-----一对一关联
- Hibernate(九)实现一对一关联映射关系
- hibernate一对一关联映射--
- hibernate一对一关联映射
- hibernate一对一关联映射
- 【Hibernate】一对一关联映射
- 【Hibernate】一对一关联映射
- QtDBus编程详解
- android 使用RadioGroup模拟 ViewpagerIndicator
- 第一章例题17年龄排序学UVa11462(内存受限问题,计数排序)
- 职场分享:职场中如何提升我们的沟通表达能力
- 今日作息及食谱(7.24)
- Hibernate关联关系映射(一对一关联)
- jQuery学习全面总结二
- [教程] CSS的优化
- 极简单的员工工资系统
- 大数据时代的机器学习
- Unity3d + NGUI 的多分辨率适配
- HDOJ 题目1715 大菲薄数 大数
- crontab
- Vision引擎组件系统中玩家角色与区块阴影介绍