========================== (不加referencedColumnName的时候) ==================
@JoinColumn(name="dividend_id")的情况
@Entity
@Table(name="wxsc_order")
public class Order extends EntitySupport {
private static final long serialVersionUID = 1L;
@Column(name="order_id")
private String orderId;//订单号
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)//让Dividend去维护外键关系
@JoinColumn(name="dividend_id")
private Dividend dividend;//赠品
@Entity()
@Table(name="wxsc_dividend")
public class Dividend extends EntitySupport {
private static final long serialVersionUID = 1L;
@Column(name="dividend_id")
private String dividendId;//赠品ID
@Column(name="dividend_name")
private String dividendName;//赠品名称
@Column(name="dividend_number")
private String dividendNumber;//赠品编号
private String attribute;//赠品属性
private int quantity;//数量
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="dividend")//让这个外键生成到order表当中。
@JoinColumn(name="order_id")
private Order order;
其映射的关系如下:
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_id` 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 DEFAULT CHARSET=utf8
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_id` 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 DEFAULT CHARSET=utf8
加了referencedColumnName之后
@JoinColumn(name="dividend_id",referencedColumnName="dividendId")
的情况
@Entity
@Table(name="wxsc_order")
public class Order extends EntitySupport {
private static final long serialVersionUID = 1L;
@Column(name="order_id")
private String orderId;//订单号
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)//让Dividend去维护外键关系
@JoinColumn(name="dividend_id",referencedColumnName="dividend_id")
private Dividend dividend;//赠品
@Entity()
@Table(name="wxsc_dividend")
public class Dividend extends EntitySupport {
private static final long serialVersionUID = 1L;
@Column(name="dividend_id")
private String dividendId;//赠品ID
@Column(name="dividend_name")
private String dividendName;//赠品名称
@Column(name="dividend_number")
private String dividendNumber;//赠品编号
private String attribute;//赠品属性
private int quantity;//数量
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="dividend")//让这个外键生成到order表当中。
@JoinColumn(name="order_id",referencedColumnName="order_id")
private Order order;
注意:如果赠品是写成(不加@JoinColumn的情况):
private String dividendId;//赠品ID
这个时候映射的时候就应该写成:
@JoinColumn(name="dividend_id",referencedColumnName="dividendId")
private Dividend dividend;//赠品
如果赠品是写成:
@Column(name="dividend_id")
private String dividendId;//赠品ID
这个时候映射的时候就应该写成:
@JoinColumn(name="dividend_id",referencedColumnName="dividend_id")
private Dividend dividend;//赠品
这两个第一个是给每取名字的取名字,取了名字的就叫名字。
第二个referencedColumnName就是指定这个关系是要映射到对方那个表的哪个字段上去的。
两边都加了 referencedColumnName 的时候结果如下:
mysql> show 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_id` varchar(255) DEFAULT NULL,
`dividend_name` varchar(255) DEFAULT NULL,
`dividend_number` varchar(255) DEFAULT NULL,
`quantity` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `dividend_id` (`dividend_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
mysql> show create table wxsc_order;
CREATE TABLE `wxsc_order` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`createTime` datetime NOT NULL,
`updateTime` datetime NOT NULL,
`version` int(11) NOT NULL,
`consumer_remarks` longtext,
`delivery_time` datetime DEFAULT NULL,
`express_number` varchar(255) DEFAULT NULL,
`freight_amount` float DEFAULT NULL,
`invoice` varchar(255) DEFAULT NULL,
`invoice_content` longtext,
`invoice_head` longtext,
`invoice_type` int(11) DEFAULT NULL,
`order_id` varchar(255) DEFAULT NULL,
`order_state` int(11) DEFAULT NULL,
`order_time` datetime DEFAULT NULL,
`payment_confirm_time` datetime DEFAULT NULL,
`payment_time` datetime DEFAULT NULL,
`seller_remarks` longtext,
`total_sum` float DEFAULT NULL,
`dividend_id` varchar(255) DEFAULT NULL,
`subscriber_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `order_id` (`order_id`),
KEY `FKD5D59A802C2E77E7` (`subscriber_id`),
KEY `FKD5D59A804768DDC7` (`dividend_id`),
CONSTRAINT `FKD5D59A804768DDC7` FOREIGN KEY (`dividend_id`) REFERENCES `wxsc_dividend` (`dividend_id`),
CONSTRAINT `FKD5D59A802C2E77E7` FOREIGN KEY (`subscriber_id`) REFERENCES `wxsc_subscriber` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)