hibernate 一对一双向关联_外键
这种情况是多对一的特例,即外键不准重复。
?
?
1.单向一对一
Person类:
package com.bjpowernode.hibernate.domain;public class Person {private int id;private String name;private IdCard idCard;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public IdCard getIdCard() {return idCard;}public void setIdCard(IdCard idCard) {this.idCard = idCard;}}
?
Person配置文件:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.bjpowernode.hibernate.domain.Person" table="t_person"><id name="id"><generator unique="true"/></class></hibernate-mapping>
?
?
?
?
?
IdCard类:
package com.bjpowernode.hibernate.domain;public class IdCard {private int id;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}/*public Set getStudents() {return students;}public void setStudents(Set students) {this.students = students;}*/}
?
IdCard配置文件:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.bjpowernode.hibernate.domain.IdCard" table="t_idcard"><id name="id"><generator name="code">package com.bjpowernode.hibernate.domain;public class IdCard {private int id;private String name;private Person person;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}/*public Set getStudents() {return students;}public void setStudents(Set students) {this.students = students;}*/public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}}
?
?
IdCard配置文件:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.bjpowernode.hibernate.domain.IdCard" table="t_idcard"><id name="id"><generator property-ref="idCard"/></class></hibernate-mapping>
?
至此,双向一对一,外键策略完成。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?