问题
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
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;
Now, the question is will DireccionEmpresa constraints be checked by BeanValidation when saving Empresa?? Thanks a lot.
回答1:
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;
.
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:
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.
So the constraints at DireccionEmpresa
will only be validated automatically when persisting an instance of that type.
来源:https://stackoverflow.com/questions/7774413/bean-validation-through-jpa-relationships