`

今天测试Hibernate映射的结果。

 
阅读更多



/***************************************************************************************************************************************************/

/*******************                           情况 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                                                                                                                                                                                                                                                                                                                                                                                                              |
+---------------+---------------------------------------------------------------------------------
| wxsc_dividend | CREATE TABLE `wxsc_dividend` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `createTime` datetime NOT NULL,
  `updateTime` datetime NOT NULL,
  `version` int(11) NOT NULL,
  `attribute` varchar(255) DEFAULT NULL,
  `dividend_name` varchar(255) DEFAULT NULL,
  `dividend_number` varchar(255) DEFAULT NULL,
  `quantity` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk |
+---------------+---------------------------------------------------------------------------------
1 row in set (0.00 sec)

会得到下面的结果。


mysql> select * from wxsc_dividend;
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
| id | createTime          | updateTime          | version | attribute | dividend_name | dividend_number | quantity |
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
|  1 | 2014-04-05 16:45:31 | 2014-04-05 16:45:31 |       0 | 玩具类    | 赠品:自行车   | 484545488       |        2 |
|  2 | 2014-04-05 16:45:31 | 2014-04-05 16:45:31 |       0 | 玩具类    | 赠品:自行车   | 484545488       |        2 |
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
2 rows in set (0.00 sec)

mysql> select * from wxsc_product;
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
| id | createTime          | updateTime          | version | article_number | order_quantity | product_attribute | product_name | unit_price | dividend_id |
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
|  1 | 2014-04-05 16:45:31 | 2014-04-05 16:45:31 |       0 | 03558866-24-22 |              0 | 湖南纸业出品      | 卫生纸       |      14.32 |           1 |
|  2 | 2014-04-05 16:45:31 | 2014-04-05 16:45:31 |       0 | 0694412548522  |              0 | 广东阳江          | 水果刀       |        6.5 |           2 |
|  3 | 2014-04-05 16:45:31 | 2014-04-05 16:45:31 |       0 | 8754515484522  |              0 | 广州经济开发区    | 茉莉蜜茶     |          3 |        NULL |
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
3 rows in set (0.00 sec)





/***************************************************************************************************************************************************/

/*******************                           情况 1,2,3   之情况2(单向)       ------- 【让Dividend做主表去参照Product】

/***************************************************************************************************************************************************/







修改为:(发现不能按下面这样子做,不然Product表的外键约束全是NULL)

//我们发现是 dividend 参照 product 表。所以这里 dividend 是主表。set的时候要用 dividend.setDividend(product);


@Entity
@Table(name="wxsc_product")
public class Product extends EntitySupport{

	@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="product")
	private Dividend dividend;//赠品


@Entity
@Table(name="wxsc_dividend")
public class Dividend extends EntitySupport {

	@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@JoinColumn(name="product_id",referencedColumnName="id")//我让Dividend做主表去参照Product
	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,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+--------------+-------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)

mysql> show create table wxsc_dividend;
+---------------+------------------------------------------------------------------------------------------------------------------
| Table         | Create Table
+---------------+------------------------------------------------------------------------------------------------------------------
| wxsc_dividend | CREATE TABLE `wxsc_dividend` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `createTime` datetime NOT NULL,
  `updateTime` datetime NOT NULL,
  `version` int(11) NOT NULL,
  `attribute` varchar(255) DEFAULT NULL,
  `dividend_name` varchar(255) DEFAULT NULL,
  `dividend_number` varchar(255) DEFAULT NULL,
  `quantity` int(11) NOT NULL,
  `product_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FKF06C177DE93A980F` (`product_id`),
  CONSTRAINT `FKF06C177DE93A980F` FOREIGN KEY (`product_id`) REFERENCES `wxsc_product` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+---------------+------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)

mysql>

插入数据试试看。结果如下:



mysql> select * from wxsc_product;
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+
| id | createTime          | updateTime          | version | article_number | order_quantity | product_attribute | product_name | unit_price |
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+
|  1 | 2014-04-05 16:58:53 | 2014-04-05 16:58:53 |       0 | 03558866-24-22 |              0 | 湖南纸业出品      | 卫生纸       |      14.32 |
|  2 | 2014-04-05 16:58:53 | 2014-04-05 16:58:53 |       0 | 0694412548522  |              0 | 广东阳江          | 水果刀       |        6.5 |
|  3 | 2014-04-05 16:58:53 | 2014-04-05 16:58:53 |       0 | 8754515484522  |              0 | 广州经济开发区    | 茉莉蜜茶     |          3 |
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+
3 rows in set (0.00 sec)

mysql> select * from wxsc_dividend;
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+------------+
| id | createTime          | updateTime          | version | attribute | dividend_name | dividend_number | quantity | product_id |
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+------------+
|  1 | 2014-04-05 16:58:53 | 2014-04-05 16:58:53 |       0 | 玩具类    | 赠品:自行车   | 484545488       |        2 |       NULL |
|  2 | 2014-04-05 16:58:53 | 2014-04-05 16:58:53 |       0 | 玩具类    | 赠品:自行车   | 484545488       |        2 |       NULL |
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+------------+
2 rows in set (0.00 sec)

mysql>

结果还是一样的。

eclipse生成如下的 Hibernate SQL 语句:

正在构造一些假数据.
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Product
        */ insert 
        into
            wxsc_product
            (createTime, updateTime, version, article_number, order_quantity, product_attribute, product_name, unit_price) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Dividend
        */ insert 
        into
            wxsc_dividend
            (createTime, updateTime, version, attribute, dividend_name, dividend_number, product_id, quantity) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Product
        */ insert 
        into
            wxsc_product
            (createTime, updateTime, version, article_number, order_quantity, product_attribute, product_name, unit_price) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Dividend
        */ insert 
        into
            wxsc_dividend
            (createTime, updateTime, version, attribute, dividend_name, dividend_number, product_id, quantity) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Product
        */ insert 
        into
            wxsc_product
            (createTime, updateTime, version, article_number, order_quantity, product_attribute, product_name, unit_price) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)




可见外键没参照到product表。这是因为写java代码的时候不可以继续使用上面那段了

//p1.setDividend(dividend); 特别说明:这种写法只适用于 product参照dividend表的情况下使用。如果参照关系反过来了。就要使用 dividend.setDividend(p1);

还有特别注意一个规律:

只要是mappBy=“product”
如果改成下面的Java语句试试看。参照关系仍然是 product 参照 dividend 





/***************************************************************************************************************************************************/

/*******************                           情况 1,2,3   之情况3(双向)       ------- 回到最初【让 Product 做主表去参照 Dividend】

/***************************************************************************************************************************************************/

set对象关系的时候一定要记住两个事情:
1.谁做主表?
2.单向还是双向映射?如果是双向就要两个对象都要互相set对方。这样级联更新的时候结果才是正确的。




@Entity
@Table(name="wxsc_product")
public class Product extends EntitySupport{

	@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@JoinColumn(name="dividend_id",referencedColumnName="id")//我让 Product 做主表去参照 Dividend 
	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);
	
	//与上面的区别就是多加了下面这两句.
	dividend.setProduct(p1);
	dividend.setProduct(p2);

	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=5 DEFAULT CHARSET=gbk |
+--------------+-----------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)

mysql> show create table wxsc_dividend;
+---------------+----------------------------------------------------------------------------------------------------
| Table         | Create Table
+---------------+----------------------------------------------------------------------------------------------------
| wxsc_dividend | CREATE TABLE `wxsc_dividend` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `createTime` datetime NOT NULL,
  `updateTime` datetime NOT NULL,
  `version` int(11) NOT NULL,
  `attribute` varchar(255) DEFAULT NULL,
  `dividend_name` varchar(255) DEFAULT NULL,
  `dividend_number` varchar(255) DEFAULT NULL,
  `quantity` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk |
+---------------+----------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)

mysql>

插入的数据如下:

mysql> select * from wxsc_product;
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
| id | createTime          | updateTime          | version | article_number | order_quantity | product_attribute | product_name | unit_price | dividend_id |
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
|  1 | 2014-04-05 17:33:59 | 2014-04-05 17:33:59 |       0 | 0694412548522  |              0 | 广东阳江          | 水果刀       |        6.5 |           1 |
|  2 | 2014-04-05 17:33:59 | 2014-04-05 17:33:59 |       0 | 03558866-24-22 |              0 | 湖南纸业出品      | 卫生纸       |      14.32 |           1 |
|  3 | 2014-04-05 17:33:59 | 2014-04-05 17:33:59 |       0 | 0694412548522  |              0 | 广东阳江          | 水果刀       |        6.5 |           2 |
|  4 | 2014-04-05 17:33:59 | 2014-04-05 17:33:59 |       0 | 8754515484522  |              0 | 广州经济开发区    | 茉莉蜜茶     |          3 |        NULL |
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
4 rows in set (0.00 sec)

mysql> select * from wxsc_dividend;
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
| id | createTime          | updateTime          | version | attribute | dividend_name | dividend_number | quantity |
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
|  1 | 2014-04-05 17:33:59 | 2014-04-05 17:33:59 |       0 | 玩具类    | 赠品:自行车   | 484545488       |        2 |
|  2 | 2014-04-05 17:33:59 | 2014-04-05 17:33:59 |       0 | 玩具类    | 赠品:自行车   | 484545488       |        2 |
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
2 rows in set (0.00 sec)

mysql>



太悲剧了。更加乱。


eclipse的输出如下:

正在构造一些假数据.
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Dividend
        */ insert 
        into
            wxsc_dividend
            (createTime, updateTime, version, attribute, dividend_name, dividend_number, quantity) 
        values
            (?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Product
        */ insert 
        into
            wxsc_product
            (createTime, updateTime, version, article_number, dividend_id, order_quantity, product_attribute, product_name, unit_price) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Product
        */ insert 
        into
            wxsc_product
            (createTime, updateTime, version, article_number, dividend_id, order_quantity, product_attribute, product_name, unit_price) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Dividend
        */ insert 
        into
            wxsc_dividend
            (createTime, updateTime, version, attribute, dividend_name, dividend_number, quantity) 
        values
            (?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Product
        */ insert 
        into
            wxsc_product
            (createTime, updateTime, version, article_number, dividend_id, order_quantity, product_attribute, product_name, unit_price) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Product
        */ insert 
        into
            wxsc_product
            (createTime, updateTime, version, article_number, dividend_id, order_quantity, product_attribute, product_name, unit_price) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)




/***************************************************************************************************************************************************/

/*******************                           情况 1,2,3   之情况4(双向)       ------- 回到最初【让 Product 做主表去参照 Dividend】

/***************************************************************************************************************************************************/

我再次修改java语句为:


	Date date = new Date();
	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);
	
	dividend.setProduct(p1);
	dividend.setProduct(p2);
	
	this.merge(dividend);

得到如下数据:


mysql> select * from wxsc_product;
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
| id | createTime          | updateTime          | version | article_number | order_quantity | product_attribute | product_name | unit_price | dividend_id |
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
|  1 | 2014-04-05 17:41:58 | 2014-04-05 17:41:58 |       0 | 0694412548522  |              0 | 广东阳江          | 水果刀       |        6.5 |           1 |
+----+---------------------+---------------------+---------+----------------+----------------+-------------------+--------------+------------+-------------+
1 row in set (0.00 sec)

mysql> select * from wxsc_dividend;
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
| id | createTime          | updateTime          | version | attribute | dividend_name | dividend_number | quantity |
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
|  1 | 2014-04-05 17:41:58 | 2014-04-05 17:41:58 |       0 | 玩具类    | 赠品:自行车   | 484545488       |        2 |
+----+---------------------+---------------------+---------+-----------+---------------+-----------------+----------+
1 row in set (0.00 sec)

mysql>

可见这样做更加错。




后台eclipse的输出是:

正在构造一些假数据.
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Dividend
        */ insert 
        into
            wxsc_dividend
            (createTime, updateTime, version, attribute, dividend_name, dividend_number, quantity) 
        values
            (?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Product
        */ insert 
        into
            wxsc_product
            (createTime, updateTime, version, article_number, dividend_id, order_quantity, product_attribute, product_name, unit_price) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert com.kington.wxsc.model.db.shop.Subscriber
        */ insert 
        into
            wxsc_subscriber
            (createTime, updateTime, version, city, country, headimgurl, language, local_headimg_url, nickname, open_id, parent_subscriber_id, payment_type, province, sex, subscribe_number, subscribe_time, telephone_number) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)






/***************************************************************************************************************************************************/

/*******************                                                   情况 4,5,6    之情况4                                    **********************/

/***************************************************************************************************************************************************/


这个情况可以忽略。




分享到:
评论

相关推荐

    XDoclet 与Hibernate 映射

    总结来说,XDoclet和Ant在Java开发中扮演着重要角色,前者简化了Hibernate映射文件的管理,后者则为项目构建和自动化提供了强大支持。通过合理的利用这两者,开发者可以提高工作效率,降低出错概率,使项目管理更加...

    让Hibernate同时支持xml和注解两种映射方式

    Spring提供了`LocalSessionFactoryBean`用于配置基于XML的Hibernate映射,而`AnnotationSessionFactoryBean`则用于处理基于注解的映射。这两个类分别处理不同的映射方式,所以默认情况下,它们不能直接共存于同一个...

    hibernate测试

    **hibernate测试** 在Java世界中,Hibernate是一个强大的对象关系映射(ORM)框架,它简化了数据库操作,使得开发者可以使用面向对象的方式来处理数据。这篇关于“hibernate测试”的文章旨在深入探讨Hibernate的...

    使用Hibernate逆向生成实体类的方法(注解和hbm.xml映射文件)

    - 运行测试程序,验证Hibernate是否能够正确读取映射信息并与数据库交互。 通过以上步骤,我们不仅能够利用Hibernate逆向生成实体类,还可以灵活地选择注解映射或hbm.xml映射的方式。这种方式极大地简化了开发过程...

    Hibernate 框架测试版本

    **Hibernate 框架测试版本** Hibernate 是一个开源的对象关系映射(ORM)框架,它为Java开发人员提供了一种方便的方式来操作数据库。这个“Hibernate 框架测试版本”包含了一个简化的示例,旨在帮助初学者了解并...

    spring测试、struts测试、hibernate测试、软件测试、网站测试、利用JBuilder测试的单元测试技术

    Hibernate测试涉及的是对象关系映射(ORM)框架Hibernate的验证。测试ORM层时,我们需要确保数据持久化操作正确无误。Hibernate Test或Hibernate Tools可以辅助进行这方面的测试,包括实体验证、会话管理以及HQL查询...

    hibernate框架一对一测试案例

    本篇我们将深入探讨Hibernate中的一对一(OneToOne)关联映射,并通过一个具体的测试案例进行解析。 ### 1. 一对一关联概念 一对一关联是指两个实体类之间存在唯一的对应关系,即一个实体对应另一个实体的一个实例...

    hibernate 单元测试批处理代码

    7. **断言和异常处理**:最后,通过JUnit的断言方法(如assertEquals, assertTrue等)验证测试结果。如果在批处理过程中预期会有特定的异常抛出,可以使用@Test(expected = Exception.class)来捕获和验证异常。 8. ...

    hibernate和jdbc测试

    测试Hibernate应用时,可以使用SessionFactory创建Session对象,然后通过Session执行CRUD(创建、读取、更新、删除)操作。为了确保数据的一致性和准确性,Hibernate支持事务管理和二级缓存。 在实际测试中,我们...

    Hibernate实例 oracel数据库

    5. 使用JUnit创建测试类,初始化SessionFactory,创建Session,进行增删改查操作,并断言结果。 6. 如果有自定义测试类,根据需求编写相应的测试方法。 **总结** 这个压缩包中的HibernateTest项目是一个综合性的...

    spring mvc +hibernate注解版测试增,删,改,查 简单xml配置应用

    在IT行业中,Spring MVC和Hibernate是两个非常重要的框架,它们分别负责Web应用程序的模型-视图-控制器(MVC)架构和对象关系映射(ORM)。在这个特定的项目中,"spring mvc + hibernate注解版测试增,删,改,查 ...

    HibernateHql综合测试小程序

    本程序“HibernateHql综合测试小程序”是针对Hibernate中的HQL(Hibernate Query Language)进行的一系列功能测试,涵盖了二十多种不同的HQL语句,旨在帮助开发者深入理解和熟练运用Hibernate的查询能力。...

    Hibernate入门jar包

    - `dom4j.jar`或`jdom.jar`:XML处理库,Hibernate的HQL查询结果集转换为XML时会用到。 - `slf4j-api.jar`和相应的实现(如`slf4j-log4j12.jar`):日志记录框架,Hibernate使用它来输出日志信息,便于调试和监控...

    junit测试spring,hibernate的dao层代码

    `Spring` 和 `Hibernate` 分别是Java企业级应用中的两大支柱,`Spring` 提供了一个强大的依赖注入容器和服务管理,而 `Hibernate` 是一个优秀的对象关系映射(ORM)框架,简化了数据库操作。将 `JUnit` 与 `Spring`...

    springMvc+hibernate Demo

    在 "Demo" 文件中,开发者可能已经创建了一个简单的示例,包括数据库配置、实体类、DAO、Service 和 Controller,展示了如何通过 Spring MVC 控制器接收请求,利用 Hibernate 进行数据库操作,并将结果返回给前端...

    MyEclipse Hibernate 快速入门 中文版(PDF)

    - 从Database Explorer的表定义中生成Java类和Hibernate映射文件 - 使用HQL编辑器 - 创建使用Hibernate的小测试应用 - **前置条件**:建议先阅读《Database Explorer快速入门》教程,以熟悉如何创建数据库连接和...

    spring+hibernate+hsqldb 测试

    标题 "spring+hibernate+hsqldb 测试" 暗示了这是一个关于整合Spring框架、Hibernate ORM框架以及HSQLDB内存数据库的测试项目。这个项目可能用于演示如何在Java应用中设置这三个组件,以便进行数据持久化操作。下面...

    string2hibernatespring整合

    同时,需要为每个实体类创建对应的Hibernate映射文件(`.hbm.xml`)。 4. **实体类和DAO层**:定义实体类,这些类对应数据库中的表,使用Hibernate的注解进行ORM配置。创建DAO(Data Access Object)接口和实现,...

Global site tag (gtag.js) - Google Analytics