验证不适用于EntityManager.merge() [英] Validation doesn't work on EntityManager.merge()
问题描述
我的实体验证很少,比如 @NotNull
,还有一些代,比如
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;
但是当调用 EntityManager.merge()
时此值不会生成。带有 @NotNull
注释的空字段在没有任何抱怨的情况下传递。即使 id
也没有生成。
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?
推荐答案
除了kraftan的回答:
In addition to kraftan's answer:
-
默认情况下,如果验证提供程序存在于环境中,JPA 2.0中的自动Bean验证将起作用,否则它将无法正常工作。您可以添加
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>
到 persistence.xml
以便生成如果找不到验证提供程序,则会出错。
to persistence.xml
in order to generate an error if validation provider is not found.
这篇关于验证不适用于EntityManager.merge()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!