zoukankan      html  css  js  c++  java
  • hibernate开发中遇到多对多的问题,可以转换为两个一对多

    例如,现在有商品表,订单表,两者是多对多的关系,

    --订单表
    create table ORDERS
    (
    ID            NUMBER(8) not null,
    TOTAL         NUMBER(8,2) not null,
    CREATED_DATE VARCHAR2(40) not null,
    REALNAME      VARCHAR2(20),
    TELPHONE      VARCHAR2(20),
    MOBILE        NUMBER(20),
    ADDRESS       VARCHAR2(200) not null,
    POSTCODE      VARCHAR2(12),
    STATE         NUMBER(1),
    ACCOUNT_ID    VARCHAR2(40),
    PAYMENT_ID    NUMBER(4),
    DELIVERY_ID   NUMBER(4),
    PAYMENT_STATE NUMBER(1),
    MEMO          VARCHAR2(400)
    )

    --商品表

    create table PRODUCT
    (
    ID           NUMBER(8) not null,
    NAME         VARCHAR2(200) not null,
    CREATED_DATE VARCHAR2(40),
    DESCRIPTION VARCHAR2(4000),
    IMAGEURL     VARCHAR2(200),
    PRICE        NUMBER(6,2) not null,
    STOCK        NUMBER(8),
    STATE        NUMBER(1) not null,
    CATEGORY_ID NUMBER(4) not null,
    DISCOUNT     NUMBER(2)
    )

    --中间表

    create table ORDERITEM
    (
    ORDER_ID     NUMBER(8),
    PRODUCT_ID   NUMBER(8),
    ORIGIN_PRICE NUMBER(6,2),
    PRICE        NUMBER(6,2),
    QUANTITY     NUMBER(6),
    ID           VARCHAR2(40) not null
    )

    传统的多对多映射会在order.hbm.xml中这样设置:

          <set name="products" table="orderitem">
            <key column="order_id"></key>
            <many-to-many class="com.shop.model.Product">
               <column name="product_id"></column>
            </many-to-many>
            </set>

    但是,此时中间表orderitem中多了三个字段,此时就不能用上面这种方法了,

    那么就可以转化成两个一对多(或者说两个多对一)

    步骤:

    1.生成orderitem的POJO类,并改造成如下内容:

    public class Orderitem implements java.io.Serializable {

    // Fields

    private String id;
    private Double originPrice;
    private Double price;
    private Long quantity;

    private Order order;
    private Product product;

    // Constructors

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

    // Property accessors

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

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

    public Double getOriginPrice() {
       return this.originPrice;
    }

    public void setOriginPrice(Double originPrice) {
       this.originPrice = originPrice;
    }

    public Double getPrice() {
       return this.price;
    }

    public void setPrice(Double price) {
       this.price = price;
    }

    public Long getQuantity() {
       return this.quantity;
    }

    public void setQuantity(Long quantity) {
       this.quantity = quantity;
    }

    public Order getOrder() {
       return order;
    }

    public void setOrder(Order order) {
       this.order = order;
    }

    public Product getProduct() {
       return product;
    }

    public void setProduct(Product product) {
       this.product = product;
    }

    }

    2.配置映射文件

    <?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">
    <!-- 
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.shop.model.Orderitem" table="ORDERITEM" schema="STORE">
            <id name="id" type="java.lang.String">
                <column name="ID" length="10" />
                <generator class="uuid.hex"></generator>
            </id>
            <property name="originPrice" type="java.lang.Double">
                <column name="ORIGIN_PRICE" precision="6" />
            </property>
            <property name="price" type="java.lang.Double">
                <column name="PRICE" precision="6" />
            </property>
            <property name="quantity" type="java.lang.Long">
                <column name="QUANTITY" precision="6" scale="0" />
            </property>
           

           <many-to-one name="order" class="com.shop.model.Order"
            cascade="save-update" >
            <column name="order_Id"></column>
            </many-to-one>
            <many-to-one name="product" class="com.shop.model.Product">
              <column name="product_Id"></column>
            </many-to-one>
        </class>
    </hibernate-mapping>

    3.接下来就变成多对一事务操作了(当然,从另一个方向看,就是一对多了)。

  • 相关阅读:
    python函数
    python数据类型补充,copy知识点及文件的操作
    python数据类型
    python介绍
    Linux基础2-2 基础文件管理命令
    Linux基础2-1 根文件系统分析
    Linux基础1-3 命令使用帮助的获取
    Linux基础1-2 ls、cd、date、clock、cal、echo、printf命令使用简介
    Linux基础1-1
    fastDFS环境搭建
  • 原文地址:https://www.cnblogs.com/fengjian/p/2582117.html
Copyright © 2011-2022 走看看