如何避免删除和重新插入子记录?
例如:我在子表中有 10 行,我想添加第 11 行。但看起来休眠正在删除所有旧的子记录并重新插入新记录。
请帮忙。
我的代码看起来像这样
@Entity
@Table(name = "ACCESS_GROUP")
public class AccessGroup implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false ,unique =true)
private String group_name;
@SuppressWarnings("deprecation")
@OneToMany(cascade=CascadeType.ALL)
@Cascade (value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name="ID", nullable=false)
@OrderBy("im")
private Set<GroupIm> imSet = null;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "accessGroup")
private Set<accessGroupList> accessGroupLists;
.....}
而孩子是这样的
@Entity
@Table(name = "ACCESS_GROUP_IM")
public class AccessGroupIm implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String im;
public accessGroupIm() {
// empty constructor for hibernate
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getIm() {
return im;
}
public void setIm(String im) {
this.im = im;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((im == null) ? 0 : im.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
accessGroupIm other = (accessGroupIm) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (im == null) {
if (other.im != null)
return false;
} else if (!im.equals(other.im))
return false;
return true;
}
}
谢谢