码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate 单项多对1

时间:2017-11-05 23:35:05      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:ack   .so   public   .hbm.xml   ima   src   ring   文件   无法   

技术分享

多的1方的Java类。 把1的作为一个属性放到多的里面。

package com.hibernate.n21;

public class Order {
    private Integer orderId;
    private String orderName;
    private Customer customer;
    private Integer getOrderId() {
        return orderId;
    }
    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }
    public String getOrderName() {
        return orderName;
    }
    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    public Order(String orderName, Customer customer) {
        super();
        this.orderName = orderName;
        this.customer = customer;
    }
    
}

映射文件:

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

<hibernate-mapping package="com.hibernate.n21">

    <class name="Order" table="orders" >
        
        <id name="orderId" type="java.lang.Integer">
            <column name="order_Id" />
            <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
            <generator class="native" />
        </id>
    
        <property name="orderName"
            type="java.lang.String" column="order_Name" >
        </property>
        
        <!-- 映射n-1的关联关系  
        name:1的属性名。把1当成多的一个属性。
        class 1的类名。
        column 1的主键名。
--> <many-to-one name="customer" class="Customer" column="customer_Id"></many-to-one> </class> </hibernate-mapping>

1的类名:

package com.hibernate.n21;

public class Customer {
    private Integer customerId;
    private String customerName;
    
    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public Customer() {
        super();
    }

    public Customer(String customerName) {
        super();
        this.customerName = customerName;
    }
    
    
}

映射文件:

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

<hibernate-mapping package="com.hibernate.n21">

    <class name="Customer" table="customer" >
        
        <id name="customerId" type="java.lang.Integer">
            <column name="customer_Id" />
            <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
            <generator class="native" />
        </id>
    
        <property name="customerName" 
            type="java.lang.String" column="customer_Name" >
        </property>
        
    </class>
    
</hibernate-mapping>

然后在主的映射文件hibernate.cfg.xml文件中加上映射

     <mapping resource="com/hibernate/n21/Customer.hbm.xml"/>
        <mapping resource="com/hibernate/n21/Order.hbm.xml"/>

 SAVE():

 1 先保存1的一端  只是三条insert语句。
 2 @org.junit.Test
 3     public void testN21Save(){
 4         Customer customer=new Customer();
 5         customer.setCustomerName("C1");
 6         Order order1 =new Order();
 7         order1.setOrderName("O1");
 8         Order order2 =new Order();
 9         order2.setOrderName("O2");
10         
11         //设置关联关系。就是给多的一方设置外键
12         order1.setCustomer(customer);
13         order2.setCustomer(customer);
14         session.save(customer);
15         session.save(order1);
16         session.save(order2);
17         
18     }
 1 先保存多的一端。3条insert语句。2条update语句
因为在插入多的一端时,无法确定1的一端的外键值。所以只能先为null。等1的一端插入后。再进行修改。
2 @org.junit.Test 3 public void testN21Save(){ 4 Customer customer=new Customer(); 5 customer.setCustomerName("C1"); 6 Order order1 =new Order(); 7 order1.setOrderName("O1"); 8 Order order2 =new Order(); 9 order2.setOrderName("O2"); 10 11 //设置关联关系。就是给多的一方设置外键 12 order1.setCustomer(customer); 13 order2.setCustomer(customer); 14 15 session.save(order1); 16 session.save(order2); 17 session.save(customer); 18 }

 

Hibernate 单项多对1

标签:ack   .so   public   .hbm.xml   ima   src   ring   文件   无法   

原文地址:http://www.cnblogs.com/bulrush/p/7789010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!