联合主键类:
表
user(username,birth,username,password)//username,birth联合主键
类
User ( userid,username,password )
Userid(username,birth)
映射文件
User.hbm.xml:
....
<composite-id name=”userid” class=”../../Userid”>
<key-property name=”birth”.....>
<column name=”birth”../>
</key-property >
<key-property name=”username”.....>
<column name=”username”../>
</key-property >
</composite-id>
....
Set集合映射
表
user(id,username,password)
Email(userid,email)
类
User ( id,username,password email)
映射文件
User.hbm.xml:
.....
<set name=”email” table=”email”>
<key column=”userid” foreign-key=”id””>
<element type=”java.lang.String” column=”email”>
</element></set>
组建映射:
表
user(id,username,password,email,address,postcode,mobile phone)
类
User ( id,username,password,profile)
Profile(email,address,postcode,mobile phone)
映射文件
User.hbm.xml:
.....
.....
<component name=”profile” class=”../../Profile”>
<parent name=”user”> //双向关联同时在Profile类中加入user属性
<property ....>
<property ....>
<property ....>
<property ....>
<component>
组建集合映射:
表
Product(id,name,price,description)
Image(filename,path,image_size,product_id) //外键关联product中的id
类
Product(id,name,price,description, image)
Image(filename,path,image_size,product_id)
映射文件
Product.hbm.xml:
.....
.....
<set name=”images” table=”image”>
<key column=”product_id” foreign-key=”id” not-null=”true”>
<composite-element class=”../../Image”>
<parent name=”product”> //双向关联同时在Image类中加入product属性
<property ....>
<property ....>
<property ....>
<property ....>
</composite_element>
</set>
多对一关联:
表
Product(id,name,price,description,category_id)
Category(id,name,description)
类
Product(id,name,price,description,category)
Category(id,name,description)
映射文件
product.hbm.xml:
.....
<many-to-one name=”category” class=”../../Category”> //l两条sql 加上outer-join=”true”关联查询只需要一条sql(提升查询性能有限)
<column name=”category_id”></column>
</many-to-one>
category.hbm.xml:
...
...
一对多关联:
表
Product(id,name,price,description,category_id)
Category(id,name,description)
类
Product(id,name,price,description,category)
Category(id,name,description,products)
映射文件
category.hbm.xml:
.....
<set name=”products” >// 级联属性cascade=”save-update”/none,save-update/delete/all 默认为none 从属对象不需要保存或更改它会跟随主动方保存和更改,尽量少用
<key column=”category_id”></key>
<one-to-many class=”../../Product”/>
</set>
product.hbm.xml:
...
...
一对多,多对一单向关联时建表时不需要要建立外键。
加上外键后生成的映射文件检测到外键会自动有双向关系
一对多单向或者双向关联时:先加入一方,再加入多方,加入多方的时候外键值为空,所以要发出更新语句。加上inverse=”true”后多方加入的时候变为主动方,自动寻找外键值,不再发出多余的update语句。我们一般情况下都是在多方维护关联关系
Private Set<Product> products=new HashSet();
一对一关联:
一对一的关联的情况是可以直接使用组件映射的,但是为了性能,分在两个表里。
访问量少的情况下是可以使用组件映射的,一张表足以!
共享主键:
表
User ( id,username,password)
Profile(id ,email,address,postcode,mobile phone)
类
User ( id,username,password,profile)
Profile(id ,email,address,postcode,mobile phone,user)//信息不经常被读取
映射文件
User.hbm.xml:
...
<one-to-one name=”profile” class=”../../Profile”>
...
Profile.hbm.xml
..
<id name=”id”>
<generator class=”foreign”>
<param name=”property”>user</param>
</generator>
</id>
<one-to-one name=”user” class=”../../User”>
唯一外键:
表
User ( id,username,password)
Profile(id ,email,address,postcode,mobile phone,user_id)
类
User ( id,username,password,profile)
Profile(id ,email,address,postcode,mobile phone,user)//信息不经常被读取
映射文件
User.hbm.xml:
...
<one-to-one name=”profile” class=”../../Profile”>
...
Profile.hbm.xml
..
<many-to-one name=”user” class=”../../User” unique=”true”>
<column name=”user_id”></column>
</many-to-one>
多对多关联:
单向关联:
表
Product(id,name,price,description)
Order_item(produdt_id,order_id) //关联表(联合主键) 可以不设外键
order(id,total,description)
类
Product(id,name,price,description)
order(id,total,description,products)
映射文件
order.hbm.xml:
.....
<set name=”proudcts” table=”orderitem”>
<key column=”order_id”></key>
<many-to-many class=”../../Product” cloumn=”product_id”/>
</set>
....
...
product.hbm.xml:
...
双向关联:
表
Product(id,name,price,description)
Order_item(produdt_id,order_id) //关联表(联合主件) 设外键Myeclipse自动的完成双向外键关联
order(id,total,description)
类
Product(id,name,price,description,orders)
order(id,total,description,products)
映射文件
order.hbm.xml:
.....
<set name=”proudcts” table=”orderitem”>
<key column=”order_id”></key>
<many-to-many class=”../../Product” cloumn=”product_id”/>
</set>
....
...
product.hbm.xml:
<set name=”orders” inverse=”true” table=”orderitem”>
<key column=”product_id”></key>
<many-to-many class=”../../Order” cloumn=”order_id”/>
</set>
...