這個問題有些與JPA有關,但更多的是關於方法而不是技術,所以希望有人能夠提供建議。Spring MVC + JPA - 綁定/更新關聯實體
我正在使用Spring MVC和Hibernate來爲一個允許用戶創建產品和產品說明的網站提供支持。我有一個Product實體,它與ProductDescription具有雙向一對多關係。
如果在提交綁定到Product的實例並指定其所有ProductDescription的表單時,惡意用戶可能會輸入ProductDescriptions的僞造ID並「劫持」其他用戶的數據。解決這個問題的方法之一是始終創建ProductDescription,然後在提交表單時刪除它們,並且每次都創建新的表單。這似乎效率低下,因爲每次更新產品時都會需要額外的刪除和寫入操作(即使ProductDesciptions未更改)。
另一種選擇是在運行更新之前檢查子實體的「所有權」。
其他人如何解決此問題?大多數人是否會刪除/插入或選擇性更新?
這裏是我說的那種POST提交的例子:
id=1
name=My Product
descriptions[0].id=123
descriptions[0].text=A lovely description of my product
descriptions[0].price=100
descriptions[1].id=123
descriptions[1].text=Another lovely description of my product in another language
descriptions[1].price=50
而且我說的那種類的一個例子:
public class Product
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToMany(mappedBy = "product")
private Set<ProductDescription> descriptions;
private String name;
}
public class ProductDescription
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private Integer price;
@ManyToOne
private Product product;
private String text;
}
我想這就是我們要做的。我們支持PHP表示層,因此不能直接訪問會話,否則我們可以將其綁定並使用會話羣集。 –