我正在尝试做一个非常简单的删除操作,但不知何故它不起作用,因为我将 DAO 更新为 JpaRepository。基本上是这样的:
A a = aRepository.findOne(id);
a.setSomeField("someNewString");
List<B> bList = a.getBs();
bList.clear();
aRepository.saveAndFlush(a);
该字段按预期更新,但bList
保持不变。我什至尝试过:
A a = aRepository.findOne(id);
a.setSomeField("someNewString");
List<B> bList = a.getBs();
for(B b : bList) {
bRepository.delete(b);
}
bRepository.flush();
bList.clear();
aRepository.saveAndFlush(a);
还是一样...
A 类如下所示:
@Entity
@Table(name = "A")
public class A implements Serializable {
private static final long serialVersionUID = -1286451120913657028L;
@Column(name = "id", length = 16, nullable = false, updatable = false)
@GenericGenerator(name = "uuid", strategy = "uuid2")
@GeneratedValue(generator = "uuid")
@Basic(fetch = FetchType.EAGER)
@Id
protected UUID id;
@OneToMany(mappedBy = "a", fetch = FetchType.EAGER)
@Cascade({ CascadeType.ALL })
List<B> bList;
// getter + setter
}
我究竟做错了什么?!
B类:
@Entity
@Table(name = "B")
public class B implements Serializable {
@Column(name = "id", length = 16, nullable = false, updatable = false)
@GenericGenerator(name = "uuid", strategy = "uuid2")
@GeneratedValue(generator = "uuid")
@Basic(fetch = FetchType.EAGER)
@Id
protected UUID id;
@ManyToOne(optional = false)
@JoinColumns({ @JoinColumn(name = "A_id", referencedColumnName = "id", nullable = false) })
@Valid
A a;
// setter + getter
}
Setter 和 getter 都非常简单:
public List<B> getBList() {
return bList;
}
public void setBList(List<B> bList) {
this.bList = bList;
}
更多信息:
- 春天 3.2.2
- 休眠 4.2.2
- 春天数据共享 1.5.1
- 弹簧数据-jpa 1.3.2