hibernate多对多,转换为两个一对多

来源:互联网 发布:手机上的编程软件 编辑:程序博客网 时间:2024/05/22 16:53

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

--订单表
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">
<keycolumn="order_id"></key>
<many-to-manyclass="com.shop.model.Product">
<columnname="product_id"></column>
</many-to-many>
</set>

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

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

步骤:

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

public class Orderitem implementsjava.io.Serializable {

// Fields

private String id;
private Double originPrice;
private Double price;
private Long quantity;
private Order order;
private Product product;

// Constructors


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"/>
<generatorclass="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" >
<columnname="order_Id"></column>
</many-to-one>
<many-to-one name="product"class="com.shop.model.Product">
<columnname="product_Id"></column>
</many-to-one>
</class>
</hibernate-mapping>

3.接下来就变成多对一事务操作了(当然,从另一个方向看,就是一对多了)
0 0
原创粉丝点击