Hibernate一对一关联映射
程序员文章站
2022-04-16 08:53:31
...
1.一对一的主键单向关联
(a)Card.hbm.xml
<?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.yyming.hibernate.Card"> <id name="id"> <generator class="native" /> </id> <property name="idCard" /> </class> </hibernate-mapping>
(b)Person.hbm.xml
<?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.yyming.hibernate.Person"> <id name="id"> <generator class="foreign" > <!-- person中的id来源于card,即共享card中的主键 --> <param name="property">card</param> </generator> </id> <property name="name" /> <!-- constrained="true",当前主键上存在一个约束,表明person表中的主键作为外键参照了card表 --> <one-to-one name="card" constrained="true"/> </class> </hibernate-mapping>
(c)Person.java和Card.java
public class Person {
private int id;
private String name;
private Card card;
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 Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
}
public class Card {
private int id;
private String idCard;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
}
2.一对一的主键双向关联
<?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.yyming.hibernate.Card"> <id name="id"> <generator class="native" /> </id> <property name="idCard" /> <one-to-one name="person"/> </class> </hibernate-mapping>
public class Card {
private int id;
private String idCard;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
}
其它的不变。以card作为主的一方,只能进行查询person,而不能级联的改变person。
3.一对一唯一外键关联(单向关联)
一对一唯一外键关联实际上是多对一关联映射的特例
可以采用<many-to-one>标签,指定多的一端unique="true",即限制了多的一端的多重性为一。
(person.hbm.xml)
<?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.yyming.hibernate.Person"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <many-to-one name="card" unique="true"/> </class> </hibernate-mapping>
(Card.hbm.xml)
<?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.yyming.hibernate.Card"> <id name="id"> <generator class="native" /> </id> <property name="cardNo" /> </class> </hibernate-mapping>