[英]catching ConstraintViolationException from jpa gives constraint name null value
I use jpa and hibernate to persist my data.My model is 我使用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;
}
I am catching the ConstraintViolationException to catch if a user is violating a unique value. 如果用户违反了唯一值,我将捕获ConstraintViolationException以进行捕获。 But when I try to access the constraintName using ConstraintViolationException.getConstraintName() gives me null value. 但是当我尝试使用ConstraintViolationException.getConstraintName()访问constraintName时,会给我空值。 How can I know which column violated the constraint so to warn the user? 我怎么知道哪一列违反了约束以警告用户?
尝试: cve.getCause().getMessage()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.