hibernate之一对一关联映射
程序员文章站
2022-04-16 09:25:22
...
Hibernate的一对一关联映射分为:
- 主键映射:
一对一的主键关联,就是两张表通过主键形成一对一关系,并且两张表共享一个主键;
实例:
一个人只能拥有一张身份证,同时一张身份证也只能属于它的一个主人;人和身份证就拥有同一个主键;
TPerson:
public class TPerson implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private TCard tcard;
get()/set()
}
TPerson.hbm.xml:
<hibernate-mapping package="com.keith.one2one.pk"> <class name="TPerson" table="TPerson"> <id name="id"> <generator class="native"> </generator> </id> <property name="name" /> <!-- 通过one-to-one节点,将TPerson与TCard类相关联 ; cascade="all" 级联关系,当主控方发生操作或改变的时候,关联对象也会执行同一动作; outer-join="true" 外连接; --> <one-to-one name="tcard" class="com.keith.one2one.pk.TCard" cascade="all" outer-join="true"/> </class> </hibernate-mapping>
TCard:
public class TCard implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer cardId;
private String num;
private TPerson tperson;
get()/set()
}
TCard.hbm.xml:
<hibernate-mapping package="com.keith.one2one.pk"> <class name="TCard" table="TCard"> <id name="cardId"> <!-- 由于采用了主键关联方式,那么通过主键关联的两个表,其关联记录的主键值必须保持同步, 意味着,我们只需为一张表设定主键生成器,而另一张表与之共享即可; --> <generator class="foreign"> <param name="property">tperson</param> </generator> </id> <property name="num" /> <!-- constrained:约束,告知Hibernate当前表主键上存在一个约束; --> <one-to-one name="tperson" class="com.keith.one2one.pk.TPerson" constrained="true"/> </class> </hibernate-mapping>
测试代码:添加数据:
TPerson person = new TPerson();
person.setName("keith");
TCard card = new TCard();
card.setNum("10101010101");
person.setTcard(card);
card.setTperson(person);
session.save(person);
session.save(card);
查询数据:
TPerson person = (TPerson) session.load(TPerson.class, new Integer(1));
System.out.println("person's name:"+person.getName());
System.out.println("person's cardNum:"+person.getTcard().getNum());
删除数据:
TPerson person = (TPerson) session.load(TPerson.class, new Integer(1));
System.out.println("person's name:"+person.getName());
System.out.println("person's cardNum:"+person.getTcard().getNum());
- 唯一外键映射;
通过外键与其进行关联;
实例:
用户和组的关系;一个用户属于一个组,一个组里有用户;
TUser:
public class TUser implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer userid;
private String name;
private TGroup group;
get()/set()
}
TGroup:
public class TGroup implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer groupId;
private String name;
get()/set()
}
TUser.hbm.xml:
<hibernate-mapping package="com.keith.one2one.fk.uni"> <class name="TUser" table="TUser"> <id name="userid"> <generator class="native"/> </id> <property name="name" /> <!-- 唯一外键关联的一对一关系,是特殊的多对一关系; 所以用many-to-one标签;并且加上unique并设置其值为true; --> <many-to-one name="group" class="TGroup" column="group_id" unique="true"/> </class> </hibernate-mapping>
TGroup.hbm.xml:
<hibernate-mapping package="com.keith.one2one.fk.uni"> <class name="TGroup" table="TGroup"> <id name="groupId"> <generator class="native"/> </id> <property name="name" /> </class> </hibernate-mapping>
以上只是单向关系,如果要设置成双向关系,则在TGroup中加入TUser对象,且加上get()/set(),在TGroup.hbm.xml中加上:
<one-to-one name="user" class="TUser" property-ref="group"/>
标签;
TGroup:
public class TGroup implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer groupId;
private String name;
private TUser user;
get()/set()
}
TGroup.hbm.xml:
<hibernate-mapping package="com.keith.one2one.fk.doubleSide"> <class name="TGroup" table="TGroup"> <id name="groupId"> <generator class="native"/> </id> <property name="name" /> <one-to-one name="user" class="TUser" property-ref="group"/> </class> </hibernate-mapping>
附件上有测试代码和实例代码;