hibernate配置一对多映射关系

hibernate配置一对多映射关系
刚才看了下<精通hibernate>,感觉关于映射关系,尤其是一对多,多对一,多对多关系应该是核心的东西,记录下.

业务逻辑:一个客户(Customer)对应多个订单(Order)

方式一: 双向一对多

PO:
Customer:

package net.foxlog.prj.business;

import java.io.Serializable;
import java.util.Set;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class Customer implements Serializable {

    
/** identifier field */
    
private Long id;

    
/** nullable persistent field */
    
private String name;

    
/** persistent field */
    
private Set orders;

    
/** full constructor */
    
public Customer(String name, Set orders) {
        
this.name = name;
        
this.orders = orders;
    }

    
/** default constructor */
    
public Customer() {
    }

    
/** minimal constructor */
    
public Customer(Set orders) {
        
this.orders = orders;
    }

    
public Long getId() {
        
return this.id;
    }

    
public void setId(Long id) {
        
this.id = id;
    }

    
public String getName() {
        
return this.name;
    }

    
public void setName(String name) {
        
this.name = name;
    }

    
public Set getOrders() {
        
return this.orders;
    }

    
public void setOrders(Set orders) {
        
this.orders = orders;
    }

    
public String toString() {
        
return new ToStringBuilder(this)
            .append(
"id", getId())
            .toString();
    }

}


Order:

package net.foxlog.prj.business;

import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class Order implements Serializable {

    
/** identifier field */
    
private Long id;

    
/** nullable persistent field */
    
private String orderNumber;

    
/** nullable persistent field */
    
private Customer customer;

    
/** full constructor */
    
public Order(String orderNumber, Customer customer) {
        
this.orderNumber = orderNumber;
        
this.customer = customer;
    }

    
/** default constructor */
    
public Order() {
    }

    
public Long getId() {
        
return this.id;
    }

    
public void setId(Long id) {
        
this.id = id;
    }

    
public String getOrderNumber() {
        
return this.orderNumber;
    }

    
public void setOrderNumber(String orderNumber) {
        
this.orderNumber = orderNumber;
    }

    
public Customer getCustomer() {
        
return this.customer;
    }

    
public void setCustomer(Customer customer) {
        
this.customer = customer;
    }

    
public String toString() {
        
return new ToStringBuilder(this)
            .append(
"id", getId())
            .toString();
    }

}

配置文件

Customer.hbm.xml:
xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<hibernate-mapping >

  
<class name="net.foxlog.prj.business.Customer" table="CUSTOMERS" >
    
<id name="id" type="long" column="ID">
      
<generator class="increment"/>
    
</id>

    
<property name="name" type="string" >
        
<column name="NAME" length="15" />
    
</property>


    
<set 
        
name="orders"
        cascade
="all-delete-orphan" 
        inverse
="true"
        
>
        
        
<key column="CUSTOMER_ID" />
        
<one-to-many class="net.foxlog.prj.business.Order" />
     
</set>   

<!--
    <set 
        name="orders"
        inverse="true"
        cascade="save-update" 
        >
        
        <key column="CUSTOMER_ID" />
        <one-to-many class="mypack.Order" />
     </set>   
-->
  
</class>
</hibernate-mapping>

Order.hbm.xml:
xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<hibernate-mapping >

   
<class name="net.foxlog.prj.business.Order" table="ORDERS">
     
      
<id name="id" type="long" column="ID">
        
<generator class="increment"/>
      
</id>
   
      
<property name="orderNumber" type="string" >
        
<column name="ORDER_NUMBER" length="15" />
      
</property>
      
      
<many-to-one
        
name="customer"
        column
="CUSTOMER_ID"
        class
="net.foxlog.prj.business.Customer"
        cascade
="save-update"
       
/>

    
</class>
 
</hibernate-mapping>

测试类 CustomerTest.java
package  net.foxlog.prj.business;

import  net.sf.hibernate.Session;
import  net.foxlog.prj.dao.DBUtil;
import  net.sf.hibernate.Transaction;
import  java.util.HashSet;

public   class  CustomerTest {
  
public  CustomerTest() {
  }
  
public   static   void  test(){
    
try {
      
new  CustomerTest().saveCustomerAndOrderWithCascade();
    }
catch (Exception ex){
      ex.printStackTrace();
    }
  }
  
  
// 保存级联信息 客户-订单
   public   void  saveCustomerAndOrderWithCascade()  throws  Exception{
    Session session 
=  DBUtil.currentSession();
    Transaction tx 
=   null ;
    
try {
      tx 
=  session.beginTransaction();
      Customer custmer 
=   new  Customer( " Jack " , new  HashSet());
      Order order 
=   new  Order();
      order.setCustomer(custmer);
      order.setOrderNumber(
" JackOrder001 " );
      custmer.getOrders().add(order);
      session.save(custmer);
      tx.commit();
      
    }
catch (Exception ex){
      
    }
  }
}



方式二: 单项多对一

即在Cutomer中没有Order的信息(PO和xml文件),在Order中设置Customer信息(同上)
保存客户和订单信息代码如下:

tx  =  session.beginTransaction();

      Customer customer
= new  Customer( " Jack " );
      Order order1
= new  Order( " Jack_Order001 " ,customer);
      Order order2
= new  Order( " Jack_Order002 " ,customer);

      session.save(order1);
      session.save(order2);

      tx.commit();

方法二很少用,不过有时我倒是喜欢用它,因为存储完全由自己的业务逻辑控制,也是比较方便的,以前的一个项目就是这么用,表之间的关系基本没有,相关联的信息完全自己当作一个个独立的表进行插入

你可能感兴趣的:(hibernate配置一对多映射关系)