考慮以下實體類,以用於,例如,的EclipseLink 2.0.2 - 其中所述link
屬性不是主鍵,但獨特nontheless。捕捉違反約束在JPA 2.0
@Entity
public class Profile {
@Id
private Long id;
@Column(unique = true)
private String link;
// Some more attributes and getter and setter methods
}
當我插入的記錄與該link
屬性的重複值,的EclipseLink不會引發EntityExistsException
,而是拋出一個DatabaseException
,該消息解釋說,唯一約束被侵犯。
這似乎不是非常有用,因爲不會有簡單的,與數據庫無關的方法來捕獲此異常。建議如何處理這個問題?
,我認爲有幾件事情是:
- 檢查錯誤代碼的
DatabaseException
- 我擔心這種錯誤代碼,雖然是數據庫的本地錯誤代碼; - 檢查一個
Profile
的具有用於預先link
特定值的存在 - 這顯然會導致多餘的查詢大量的。
我已經提交一個bug針對此問題。請爲此投票,以便我們解決此問題:https://bugs.eclipse.org/bugs/show_bug.cgi?id=375745 – sdoca 2012-03-30 17:01:58