服务器之家

服务器之家 > 正文

Hibernate一对多关联双向关联代码实现分享

时间:2019-10-26 16:18     来源/作者:java教程网

1、创建实体类(Customer.java、Orders.java)

 

复制代码代码如下:


package wck.stu.vo.oneToMany_single;

 

import java.util.HashSet;
import java.util.Set;

public class Customer {
    private String id = "";

    private String cName = "";

    private String bank = "";

    private String phone = "";

    private Set orders = new HashSet();

    public String getId() {
        return id;
    }

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

    public String getcName() {
        return cName;
    }

    public void setcName(String cName) {
        this.cName = cName;
    }

    public String getBank() {
        return bank;
    }

    public void setBank(String bank) {
        this.bank = bank;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Set getOrders() {
        return orders;
    }

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

    
}

Customer.java

 

 

复制代码代码如下:


package wck.stu.vo.oneToMany_single;

 

public class Orders {
    private String id = "";

    private String orderNo = "";

    private Double money = 0.0;

    private Customer customer;

    public String getId() {
        return id;
    }

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

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Customer getCustomer() {
        return customer;
    }

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

Orders.java

 

2、配置文件(Customer.hbm.xml、Orders.hbm.xml)

 

复制代码代码如下:


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

 

<hibernate-mapping>
    <class name="wck.stu.vo.oneToMany_single.Customer" table="CUSTOMER">
        <id name="id" type="java.lang.String">
            <column name="C_ID" length="32" not-null="true"></column>
            <generator class="uuid.hex"></generator>
        </id>
        <property column="CNAME" name="cName" type="string" length="100"/>
        <property column="BANK" name="bank" type="string" length="100"/>
        <property column="PHONE" name="phone" type="string" length="20"/>
        <!--一对多双向关联映射customer到orders,单的一方配置 -->
        <set cascade="all" inverse="true" lazy="false" name="orders">
            <key column="CUSTOMER_ID" />
            <one-to-many class="wck.stu.vo.oneToMany_single.Orders" />
        </set>
    </class>
</hibernate-mapping>

Customer.hbm.xml

 

 

复制代码代码如下:


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

 

<hibernate-mapping>
    <class name="wck.stu.vo.oneToMany_single.Orders" table="ORDERS">
        <id name="id" type="java.lang.String">
            <column name="C_ID" length="32" not-null="true"></column>
            <generator class="uuid.hex"></generator>
        </id>
        <property column="ORDERNO" name="orderNo" type="java.lang.String" length="17"/>
        <property column="MONEY" name="money" type="java.lang.Double" />
        <many-to-one name="customer" class="wck.stu.vo.oneToMany_single.Customer" column="CUSTOMER_ID" cascade="all" lazy="false" not-null="true"/>
    </class>
</hibernate-mapping>

Orders.hbm.xml

 

3、测试代码

 

复制代码代码如下:

public String saveCustomerInfo() {
        Customer customer = new Customer();
        customer.setcName("清华大学出版社");
        customer.setBank("9559501012356789");
        customer.setPhone("010-62772015");    
        Orders order = new Orders();
        order.setOrderNo(new Long(System.currentTimeMillis()).toString());
        order.setMoney(new Double(rnd.nextDouble()*10000));
        order.setCustomer(customer);
        customer.getOrders().add(order);
        oneToManyDao.saveCustormerInfo(customer);
        return customer.getId();
    }

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字 2019-06-05
返回顶部