0
我可以通過普通的HQL刪除任何行一樣無法刪除的持久性JPA子實體刪除
Query q = em.createQuery("DELETE FROM Cities WHERE id = :id");
但我想刪除它像
Cities city = em.getReference(Cities.class, 8);
try {
em.getTransaction().begin();
em.remove(city);
em.getTransaction().commit();
}
catch(RuntimeException e) {
e.printStackTrace();
em.getTransaction().rollback();
}
而且我沒有警告或錯誤
國家(僅限OneToMany註釋)@OneToMany(mappedBy = "country", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private List<Cities> cities;
城市(僅FK場)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "country_id", nullable = false)
private Countries country;
事情,我試圖爲:
1) trying to apply orphanRemoval = true
2) cascade = CascadeType.MERGE to Cities
3) em.refresh(city)
你說「我沒有任何警告或錯誤」,因此沒有系統工作的錯誤。那麼問題是什麼? – Namphibian
我在這些更改後達到結果: 嘗試em.getTransaction()。begin(); city.setCountries(null); em.refresh(city); em.remove(city); em.getTransaction()。commit(); } catch(RuntimeException e){ e.printStackTrace(); em.getTransaction()。rollback(); } – Nesquik27
問題是爲什麼在子實體中沒有刪除而沒有刪除FK並且像上面那樣刷新它:( – Nesquik27