标签:
一、单向多对一
举例:多个订单对应一个顾客
顾客类Customer.java:
public class Customer{ private Integer customerId; private String customerName; //get() set() 省略 }
订单类Order.java:
public class Order { private Integer orderId; private String orderName; private Customer customer; /** geter、seter 此处省略 **/ }
Customer.hbm.xml:
<class name="Customer" table="Customer"> <id name="customerId" column="COSTOMER_ID" type="integer"> <generator class="native"/><!-- 主键生成策略native 不同数据库不同 --> </id> <property name="customerName" column="CUSTOMER_NAME" type="string"></property> </class>
Order.hbm.xml:
<class name="Order" table="order"> <id name="orderId" column="ORDER_ID" type="integer"> <generator class="native"/> </id> <property name="orderName" column="ORDER_NAME" type="string"></property> <!-- 映射多对一关系 --> <many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one> </class>
二、双向多对一
顾客类 Customer.java:
public class Customer{ private Integer customerId; private String customerName; //增加Set类型属性 并初始化空的Set private Set<Order> orders = new HashSet<Order>(); //get() set() 省略 }
订单类Order.java
public class Order { private Integer orderId; private String orderName; private Customer customer; /** geter、seter 此处省略 **/ }
Customer.hbm.xml
<class name="Customer" table="Customer"> <id name="customerId" column="COSTOMER_ID" type="integer"> <generator class="native"/> </id> <property name="customerName" column="CUSTOMER_NAME" type="string"></property> <set name="orders" table="order" inverse="true"><!-- inverse设为true放弃维护关联关系 , 又另一端单独维护 --> <key column="CUSTOMER_ID"></key><!-- 指定外键字段 --> <one-to-many class="Order"/> </set> </class>
Order.hbm.xml
<class name="Order" table="order"> <id name="orderId" column="ORDER_ID" type="integer"> <generator class="native"/> </id> <property name="orderName" column="ORDER_NAME" type="string"></property> <!-- 映射多对一关系 --> <many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one> </class>
三、单向一对一
举例:一个部门Department 一个经理Manager
实体类:Department.java
public class Department{ private Integer departmentId; private String departmentName; private Manager manager; //省略geter seter; }
Manager.java
public class Manager{ private Integer managerId; private String managerName; //省略geter,seter; }
1)基于外键的单向一对一:Department.hbm.xml
<class name="Department" table="DEPARTMENTS" > <id name="departmentId" column="DEPARTMENT_ID"> <generator class="native" /> </id> <property name="departmentName" column="DEPARTMENT_NAME" type="string"> </property> <!-- 使用many-to-one 映射一对一, 一对一是一种特殊的多对一 --> <many-to-one name="manager" class="Manager" cascade="all" column="MANAGER_ID" unique="true"></many-to-one> </class>
2)基于主键的单向1对1
<class name="Department" table="DEPARTMENTS" > <id name="departmentId" column="DEPARTMENT_ID"> <generator class="foreign"> <param name="property">manager</param> </generator> </id> <property name="departmentName" column="DEPARTMENT_NAME" type="string"> </property> <one-to-one name="manager" class="Manager" cascade="all" constrained="true"></one-to-one> </class> <!-- one-to-one的单向关联中,如果constrained=false,则会在查询时就全部取出来,用left outer join的方式。如果constrained=true,hibernate即会延迟加载sql,只把主表的查出来,等有用到关联表的再发sql取。 -->
四、双向一对一
1)基于外键的双向一对一
Department.java:
public class Department{ private Integer departmentId; private String departmentName; private Manager Manager; }
Manager.java:
public class Manager{ private Integer managerId; private String managerName; private Department department; }
Department.hbm.xml:
<class name="Department" table="departments"> <id name="departmentId" column="DEPARTMENT_ID" type="integer"> <generator class="native" /> </id> <property name="departmentName" column="DEPARTMENT_NAME" type="string"> </property> <many-to-one name="manager" column="MANAGER_ID" class="Manager" unique="true"/> </class>
Manager.hbm.xml:
<class name="Manager" table="MANAGERS"> <id name="managerId" column="MANAGER_ID" type="integer"> <generator class="native" /> </id> <property name="managerName" column="MANAGER_NAME" type="string"> </property> <!-- 没有外键的一端,使用one-to-one进行映射,并指定property-ref属性为关联实体外键所对应的属性--> <one-to-one name="department" class="Department" property-ref="manager"></one-to-one> </class>
2)基于主键的双向一对一:
Department.hbm.xml:
<class name="Department" table="departments"> <id name="departmentId" column="DEPARTMENT_ID" type="integer"> <generator class="foreign"> <param name="property">manager</param> </generator> </id> <property name="departmentName" column="DEPARTMENT_NAME" type="string"> </property> <one-to-one name="manager" class="Manager" constrained="true"/> </class>
Manager.hbm.xml:
<class name="Manager" table="MANAGERS"> <id name="managerId" column="MANAGER_ID" type="integer"> <generator class="native" /> </id> <property name="managerName" column="MANAGER_NAME" type="string"> </property> <one-to-one name="department" class="Department" ></one-to-one> </class>
五、单向多对多
多对多需要一个中间表
Category.java
public class Category { private Integer id; private String name; private Set<Item> items = new HashSet<Item>(); }
Item.java
public class Item { private Integer id; private String name; }
Category.hbm.xml
<class name="Category" table="CATEGORYS"> <id name="id" column="CATEGORY_ID" type="integer"> <generator class="native"></generator> </id> <property name="name" column="CATEGORY_NAME" type="string"></property> <set cascade="all" name="items" table="CATEGORYS_ITEMS"><!--中间表的名字--> <key column="CATEGORY_ID"></key> <many-to-many class="Item" column="ITEM_ID"></many-to-many> </set> </class>
Item.hbm.xml
<class name="Item" table="ITEMS" > <id name="id" column="ITEM_ID"> <generator class="native" /> </id> <property name="name" column="ITEM_NAME" type="string"> </property> </class>
六、双向多对多
Category.java
public class Category { private Integer id; private String name; private Set<Item> items = new HashSet<Item>(); }
Item.java
public class Item { private Integer id; private String name; private Set<Category> categorys = new HashSet<Category>(); }
Category.hbm.xml
<class name="Category" table="CATEGORYS"> <id name="id" column="CATEGORY_ID" type="integer"> <generator class="native"></generator> </id> <property name="name" column="CATEGORY_NAME" type="string"></property> <set cascade="all" name="items" table="CATEGORYS_ITEMS"><!--中间表的名字--> <key column="CATEGORY_ID"></key> <many-to-many class="Item" column="ITEM_ID"></many-to-many> </set> </class>
Item.hbm.xml
<class name="Item" table="ITEMS" > <id name="id" column="ITEM_ID"> <generator class="native" /> </id> <property name="name" column="ITEM_NAME" type="string"> </property> <set name="categorys" table="CATEGORYS_ITEMS" inverse="true"><!-- 必须有一端必须设置inverse --> <key column="ITEM_ID"></key> <many-to-many class="Category" column="CATEGORY_ID"></many-to-many> </set> </class>
参考:尚硅谷Hibernate教程 http://www.atguigu.com/download.shtml
标签:
原文地址:http://www.cnblogs.com/fanxiaofan/p/4321161.html