Validation doesn't work on EntityManager.merge()
I have few validations on my entity, like @NotNull, and some generation, like
@Id @GeneratedValue(strategy = AUTO) @Column(name = "ID") private Long id; @Column @GeneratedValue(strategy = GenerationType.AUTO) private Long referenceNumber;
However when calling EntityManager.merge() this values are not generated. Null fields with @NotNull annotation are passed without any complain. Even id is not generated.
Should I switch this generation on somehow? How, and where?
In addition to kraftan's answer:
By default automatic bean validation in JPA 2.0 works if validation provider is "present in the environment", otherwise it silently doesn't work. You can add
to persistence.xml in order to generate an error if validation provider is not found.
- JPA doesn't support generation of arbitrary (non-id) properties. Some JPA providers may have extensions.
Merge() does not invoke pre-insert/pre-update event listeners by default. flush() after the merge() should do it.