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

点这里可以跳转到人工智能网站

发表评论