我試圖做一個非常簡單的刪除操作,但不知何故,它不工作,因爲我更新DAO到JpaRepository。基本上它是這樣的: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
}
getter和setter方法都一樣簡單possbile:
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
B類是什麼樣的?和A.getBs()方法? – Spiff
更新了最初的發佈。 –