エンティティクラス:
public class MyUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "name")
private String name;
// other attrs and getter-setters
public MyUser() {
}
public MyUser(Integer id) {
this.id = id;
}
public MyUser(Integer id, String name) {
this.id = id;
this.name = name;
}
}
使用コード:
MyUser myuser = new MyUser();
myuser.setName("abc");
try {
em.persist(myuser);
} catch (ConstraintViolationException e) {
System.out.println("size : " + e.getConstraintViolations().size());
ConstraintViolation<?> violation = e.getConstraintViolations().iterator().next();
System.out.println("field : " + violation.getPropertyPath().toString());
System.out.println("type : " + violation.getConstraintDescriptor().getAnnotation().annotationType());
}
出力:
INFO: size : 1
INFO: field : id
INFO: type : interface javax.validation.constraints.NotNull
環境 :
JDK 6 u23
GlassFish Server Open Source Edition 3.1-b41(bean-validator.jarがあります)
NetBeans IDE 7.0 Beta 2
質問 :
Beanバリデーターがnull許容ではないが自動生成されたidフィールドにこの例外をスローする理由について誰かが提案していますか?正しいアプローチは何ですか?