日期:2014-05-16 浏览次数:20398 次
/***************************************************************************************************************************************************/ /******************* 情况 1,2,3 之情况1(单向) ------- 【让Product做主表去参照Dividend】 /***************************************************************************************************************************************************/ 即:由 //我们发现是product参照dividend表。所以这里product是主表。set的时候要用 product.setDividend(dividend); @Entity @Table(name="wxsc_product") public class Product extends EntitySupport{ @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY) @JoinColumn(name="dividend_id",referencedColumnName="id") private Dividend dividend;//赠品 @Entity @Table(name="wxsc_dividend") public class Dividend extends EntitySupport { @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="dividend") private Product product; 以及: Dividend dividend = new Dividend(); dividend.setAttribute("玩具类"); //dividend.setDividendId("DJFOIWEFILDJFOWFEjo-DJFWOE"); dividend.setDividendName("赠品:自行车"); dividend.setDividendNumber("484545488"); dividend.setQuantity(2); dividend.setCreateTime(date); dividend.setUpdateTime(date); //下面让这两个商品p1,p2都赠送这一个赠品,另外一个商品p3则没有赠品 Product p1 = new Product(); p1.setArticleNumber("03558866-24-22"); p1.setProductAttribute("湖南纸业出品"); p1.setProductName("卫生纸"); p1.setUnitPrice(14.32d); p1.setCreateTime(date); p1.setUpdateTime(date); p1.setDividend(dividend); Product p2 = new Product(); p2.setArticleNumber("0694412548522"); p2.setProductAttribute("广东阳江"); p2.setProductName("水果刀"); p2.setUnitPrice(6.5d); p2.setCreateTime(date); p2.setUpdateTime(date); p2.setDividend(dividend); Product p3 = new Product(); p3.setArticleNumber("8754515484522"); p3.setProductAttribute("广州经济开发区"); p3.setProductName("茉莉蜜茶"); p3.setUnitPrice(3.0d); p3.setCreateTime(date); p3.setUpdateTime(date); p3.setDividend(null); this.merge(p1); this.merge(p2); this.merge(p3); 生成的表关系是: mysql> show create table wxsc_product; +--------------+--------------------------------------------------------------------------------- | Table | Create Table | +--------------+--------------------------------------------------------------------------------- | wxsc_product | CREATE TABLE `wxsc_product` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `createTime` datetime NOT NULL, `updateTime` datetime NOT NULL, `version` int(11) NOT NULL, `article_number` varchar(255) DEFAULT NULL, `order_quantity` int(11) DEFAULT NULL, `product_attribute` varchar(255) DEFAULT NULL, `product_name` varchar(255) DEFAULT NULL, `unit_price` double DEFAULT NULL, `dividend_id` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FKEC59DBA19367CD45` (`dividend_id`), CONSTRAINT `FKEC59DBA19367CD45` FOREIGN KEY (`dividend_id`) REFERENCES `wxsc_dividend` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk | +--------------+---------------------------------------------------------------------------------- 1 row in set (0.00 sec) mysql> show create table wxsc_dividend; +---------------+--------------------------------------------------------------------------------- | Table | Create Table