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;
}
}