通过JPA关系进行Bean验证 [英] Bean Validation Through JPA Relationships
问题描述
我想使用Bean验证来注释实体中的约束,现在的问题是,实体上的关系也将得到验证吗?例如,假设我具有以下实体:
I would like to use Bean Validation to annotate constraints in my entities , now the question is, will the relationships on the entities be validated as well?. For example suppose I have the following entities:
@Entity
@Table(name = "css_empresa")
public class Empresa extends EntidadContactable implements Serializable,
Convert {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "EMPRESA_ID_GENERATOR", sequenceName = ConstantesSecuencias.SEQ_EMPRESA)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMPRESA_ID_GENERATOR")
@Column(name = "cod_empresa", unique = true, nullable = false)
private Long id;
@Column(name = "num_ruc", precision = 13)
private BigDecimal numRuc;
@Column(name = "num_rup", precision = 15)
private BigDecimal numRup;
@Column(name = "txt_direccion_web", length = 255)
private String txtDireccionWeb;
@NotNull
@Column(name = "txt_nombre", nullable = false, length = 255)
private String txtNombre;
@Column(name = "txt_observaciones", length = 255)
private String txtObservaciones;
@OneToOne
@JoinColumn(name = "cod_usuario")
private Usuario administrador;
// bi-directional many-to-one association to DireccionEmpresa
@OneToMany(mappedBy = "empresa", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<DireccionEmpresa> direccionEmpresas;
--Rest of code ommited for brevity
和DireccionEmpresa实体声明如下:
and DireccionEmpresa entity is declared as follows:
@Entity
@Table(name = "css_direccion_empresa")
public class DireccionEmpresa implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "DIREMPRESA_ID_GENERATOR", sequenceName = ConstantesSecuencias.SEQ_DIREMPRESA)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DIREMPRESA_ID_GENERATOR")
@Column(name = "cod_direccion_empresa", unique = true, nullable = false)
private Long codDireccionEmpresa;
@Column(name = "sts_predeterminado", nullable = false, length = 1)
private String stsPredeterminado;
@NotNull
@Column(name = "txt_calle_principal", nullable = false, length = 120)
private String txtCallePrincipal;
@NotNull
@Column(name = "txt_calle_secundaria", length = 120)
private String txtCalleSecundaria;
@Column(name = "txt_descripcion", length = 120)
private String txtDescripcion;
@Column(name = "txt_informacion_adicional", length = 120)
private String txtInformacionAdicional;
@NotNull
@Column(name = "txt_numero", nullable = false, length = 10)
private String txtNumero;
@NotNull
@Column(name = "txt_sector", nullable = false, length = 60)
private String txtSector;
现在,问题是保存Empresa时,BeanValidation将检查DireccionEmpresa约束吗?非常感谢.
Now, the question is will DireccionEmpresa constraints be checked by BeanValidation when saving Empresa?? Thanks a lot.
推荐答案
否,持久化Empresa
的实例时,不会验证DireccionEmpresa
处的约束.
No, the constraints at DireccionEmpresa
won't be validated when persisting an instance of Empresa
.
Generally a cascaded validation will only happen when a reference (such as a plain object reference or a list) is annotated with the @Valid annotation, which isn't the case for List<DireccionEmpresa> direccionEmpresas;
.
但是,即使该字段用@Valid
注释,在JPA情况下也不会触发级联验证,如JPA 2规范在3.6.1.2节中所述:
But even if that field was annotated with @Valid
such a cascaded validation wouldn't be triggered in the JPA case as the JPA 2 spec says in section 3.6.1.2:
对于实体关联(单值或多值),不得出现验证级联(@Valid). [...这保证] [...]在给定的冲洗周期中,没有任何实体会被多次验证.
Validation cascade (@Valid) must not occur for entity associations (single- or multi-valued). [... This guarantees] that [...] no entity will be validated more than once during a given flush cycle.
因此,只有在持久保存该类型的实例时,才会自动验证DireccionEmpresa
处的约束.
So the constraints at DireccionEmpresa
will only be validated automatically when persisting an instance of that type.
这篇关于通过JPA关系进行Bean验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!