1. 程式人生 > >Hibernate--一對多/多對一

Hibernate--一對多/多對一

關聯映射 map length cte ssi 創建 get date pro

一對多關聯(多對一):

    一對多關聯映射:
        在多的一端添加一個外鍵指向一的一端,它維護的關系是一指向多
    多對一關聯映射:
        咋多的一端加入一個外鍵指向一的一端,它維護的關系是多指向一

  在配置文件中添加:    在一一端使用
<set><key></key><one-to-many></one-to-many></set>    <key>指定的外鍵字段必須和<many-to-one>指定的外鍵字段一致    在多一端使用<many-to-one>

數據庫中customer表和order表的關系:

技術分享

創建實體類:

public class Customer {

    private Integer id;
    private String name;
    private Character gender;
    private Integer age;
    private String level;

    //一對多
    private Set<Order> orders = new HashSet<Order>();
    
    public Customer() {
        
super(); }

  getters and setters }
public class Order {

    private Integer id;
    private String orderno;
    private String productName;
    
    //多對一
    private Customer customer;

    public Order() {
        super();
        // TODO Auto-generated constructor stub
    }

  getters and setters
}

創建映射文件:

<hibernate-mapping>
    <class name="com.roxy.hibernate.pojo.Customer" table="t_customer">
    
        <id name="id" column="c_id">
            <generator class="native"></generator>
        </id>
        
        <property name="name" column="c_name" not-null="true"></property>
        <property name="gender" column="c_gender" length="1"></property>
        <property name="age" column="c_age"></property>
        <property name="level" column="c_level" length="20"></property>
        
        <set name="orders">
            <key column="customer_id"></key>
            <one-to-many class="com.roxy.hibernate.pojo.Order"/>
        </set>
    </class>
</hibernate-mapping>
<hibernate-mapping package="com.roxy.hibernate.pojo">

    <!-- 類和表的映射 -->
    <class name="Order" table="t_order">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <!-- 其他屬性映射 -->
        <property name="orderno" column="orderno" length="20"></property>
        <property name="productName" column="product_name" length="100"></property>

        <!-- 多對一 -->
        <many-to-one name="customer" class="Customer" column="customer_id" />
    
    </class>
</hibernate-mapping>   

創建配置文件:

        <!-- mapping文件 -->
        <mapping resource="com/roxy/hibernate/pojo/Customer.hbm.xml"/>
        <mapping resource="com/roxy/hibernate/pojo/Order.hbm.xml"/>

查看並分析SQL語句:

Hibernate: 
       alter table t_order 
       add constraint FKesy3n2gc3fa0s3trrk3tvyv9a 
       foreign key (customer_id) 
       references t_customer (c_id)

----- hibernate首先為t_order和t_customer添加外鍵關聯
Hibernate: insert into t_customer (c_name, c_gender, c_age, c_level) values (
?, ?, ?, ?)

----- 向t_customer中插入數據(session.save(cust);)
Hibernate: insert into t_order (orderno, product_name, customer_id) values (
?, ?, ?)

----- 向t_order中插入數據(session.save(o1);)
Hibernate: insert into t_order (orderno, product_name, customer_id) values (
?, ?, ?)
----- 向t_order中插入數據(session.save(o2);)
Hibernate: update t_order set customer_id
=? where id=?
----- 維護兩個表之間的關系,hibernate更新t_customer數據
Hibernate: update t_order set customer_id=? where id=?
----- 維護兩個表之間的關系,hibernate更新t_customer數據

Hibernate--一對多/多對一