hibernate学习笔记9---映射

1、one-to-many
<set name="email" inverse="true" cascade="all">
     <key column="uid"/>
     <one-to-many class="com.chen.vo.Email"/>
</set>

2、many-to-one
<many-to-one name="users" column="uid" class="com.chen.vo.Users"/>

3、one-to-one
a)主键关联:
card表用uid作为主键,同时也是外键
Users.hbm.xml:
<one-to-one name="card" class="com.chen.vo.Card" cascade="all"/>

Card.hbm.xml:
<id name="uid" column="uid" type="int">
   <generator class="foreign">
      <param name="property">users</param>
   </generator>
</id>
<one-to-one name="users" class="com.chen.vo.Users" constrained="true"/>


b)唯一外键关联:
card表用id作为主键,uid为外键
Users.hbm.xml:
<one-to-one name="card" class="com.chen.vo.Card" cascade="all"/>

Card.hbm.xml:
<id name="id" type="java.lang.Integer">
     <column name="id" />
     <generator class="native" />
</id>
<many-to-one name="users" column="uid" class="com.chen.vo.Users" unique="true"/>


其中one-to-many,many-to-one默认延迟加载
many-to-one单向关联就可以级联保存与更新
one-to-many,one-to-one级联保存与更新一般采用双向关联

你可能感兴趣的:(java,Hibernate,xml)