日期:2014-05-16  浏览次数:20481 次

数据库映射只@OneToMany的两个区别


========================== (不加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