Validation doesn't work on EntityManager.merge()
-
29-09-2019 - |
Question
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?
Solution
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
<validation-mode>CALLBACK</validation-mode>
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.
OTHER TIPS
Merge()
does not invoke pre-insert/pre-update event listeners by default. flush()
after the merge()
should do it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow