欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-22 09:49:22
1、创建实体类(customer.java、orders.java) 复制代码 代码如下:package wck.stu.vo.onetomany_single; im...

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();
    }

上一篇:

下一篇: