@OneToMany三種設置方式詳解

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
package footmark.springdata.jpa.domain;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import org.apache.log4j.Logger;
@Entity
public class Custom {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(Custom.class);
private Long custId;
private String detail;
private Collection<Address> addresses = new ArrayList<Address>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getCustId() {
return custId;
}
public String getDetail() {
return detail;
}
/*
* 三種配置方式:
* 1. @OneToMany(cascade= {CascadeType.ALL},fetch=FetchType.LAZY ,mappedBy="customId")
* mappedBy屬性用於雙向關聯實體時使用,
* mappedBy屬性:用在雙向關聯中,把關係的維護權反轉 ; 跟hibernate XML映射中的property-ref一樣。
*
* JPA執行步驟:一、插入數據到CUSTOM表,
* 二、執行getAddresses()獲取需要持久化的ADDRESS(ADDRESS必須代碼設置外鍵CUSTID),
* 三、插入數據到ADDRESS表
*
* 2.@OneToMany(cascade= {CascadeType.ALL},fetch=FetchType.LAZY )
* JPA執行步驟: 一、插入數據到CUSTOM表,
* 二、執行getAddresses()獲取需要持久化的ADDRESS(ADDRESS代碼不需要設置外鍵CUSTID),ADDRESS和CUSTOM關係保存在關聯表中;
* 三、插入數據到ADDRESS表
* 四、插入關聯信息到CUSTOM_ADDRESS 表中
* 另外關聯表的字段對應關係也可以手工設置,
* @JoinTable(name="ref_customer_address",
* joinColumns={@JoinColumn(name="customer_id",referencedColumnName="custId")},
* inverseJoinColumns={@JoinColumn(name="address_id",referencedColumnName="addrId")}
*
* 3.@OneToMany(cascade= {CascadeType.ALL},fetch=FetchType.LAZY )
* @JoinColumn(name="customer_id") 對應的是表中的字段,會在最後一步進行該字段的更新
* 該設置屬於單向關聯, 該設置需要執行三條SQL操作,不推薦;JPA推薦第一種和第二種做法;
* JPA執行步驟如下:一、插入數據到CUSTOM表,
* 二、執行getAddresses()獲取需要持久化的ADDRESS(ADDRESS必須代碼設置外鍵CUSTID或者將外鍵屬性設置爲可以爲空),
* 三、插入數據到ADDRESS表
* 四、 然後再UPDATE ADDRESS 設置外鍵關係customer_id
*
* 4.另外多端需要初始化一個空數組
* private Collection<Address> addresses = new ArrayList<Address>();
*
* */
//級聯操作
@OneToMany(cascade= {CascadeType.ALL},fetch=FetchType.LAZY ,mappedBy="customId" )
// 雙向關聯,mappedBy="customId" 不可共存 @JoinColumn(name="customer_id")
// 2 @JoinTable(name="ref_customer_address",
// joinColumns={@JoinColumn(name="customer_id",referencedColumnName="custId")},
// inverseJoinColumns={@JoinColumn(name="address_id",referencedColumnName="addrId")}
// )
public Collection<Address> getAddresses() {
if (logger.isDebugEnabled()) {
logger.debug("getAddresses() - start"); //$NON-NLS-1$
}
// cascade屬性和mappedBy用在一起時,一定要通過調用雙方的set方法來建立關係
for (Address add :addresses)
add.setCustomId(custId);//設置ADDRESS的CUSTOM外鍵
if (logger.isDebugEnabled()) {
logger.debug("getAddresses() - end"); //$NON-NLS-1$
}
return addresses;
}
public void pushAddress(Address add){
addresses.add(add);
}
public void setAddresses(Collection<Address> addresses) {
this.addresses = addresses;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public void setDetail(String detail) {
this.detail = detail;
}
}

------------

package footmark.springdata.jpa.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Address {

private Long addrId;

private Long customId;

@Column(nullable=false)
public Long getCustomId() {
return customId;
}

public void setCustomId(Long customId) {
this.customId = customId;
}

private String detail;

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
public Long getAddrId() {
return addrId;
}

public String getDetail() {
return detail;
}

public void setAddrId(Long addrId) {
this.addrId = addrId;
}

public void setDetail(String detail) {
this.detail = detail;
}
}
發佈了38 篇原創文章 · 獲贊 11 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章