![](/img/trans.png)
[英]JPA ConstraintViolationException the forgein key as value null
[英]catching ConstraintViolationException from jpa gives constraint name null value
我使用jpa和hibernate來保存數據。我的模型是
@Entity
@Table(name = "user", uniqueConstraints=@UniqueConstraint(columnNames={"username"}, name="username"))
public class User implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "user_role", nullable = false)
private UserRole userRole;
}
如果用戶違反了唯一值,我將捕獲ConstraintViolationException以進行捕獲。 但是當我嘗試使用ConstraintViolationException.getConstraintName()訪問constraintName時,會給我空值。 我怎么知道哪一列違反了約束以警告用戶?
嘗試: cve.getCause().getMessage()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.