Bean 유효성 검사를 사용하여 내 엔티티의 제약 조건에 주석을 달고 싶습니다. 이제는 엔티티의 관계도 유효성을 검사합니까? 다음과 같이JPA 관계를 통한 bean 유효성 확인
@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 엔티티 선언 : 예를 들어, 나는 다음과 같은 기관이 있다고 가정
@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 것입니까 ?? 고마워.
따라서 유효성 검사를 수행하려면 참조를 반복하고 각 참조를 유지해야합니다. – Pablo
'CascadeType.ALL'을 사용하기 때문에 참조 된 객체는'Empresa' 인스턴스를 영속화 할 때 자동으로 지속됩니다. 이것은 차례로 유효성 검사를 트리거하므로 사용자가 직접 확인하지 않아도됩니다. 명세는 참조를 소유 한 객체를 영속화하고 참조 된 객체 자체를 영속화 할 때 참조 된 객체가 두 번 유효성이 확인되지 않도록합니다. – Gunnar
좋아, 관련 엔터티에 대한 내 참조가 '@Valid'주석으로 주석 처리되어야합니다. 즉, '@Valid '@OneToMany (mappedBy = "empresa", fetch = FetchType.LAZY, cascade = CascadeType.ALL) 개인 목록 direccionEmpresas; 은 Empresa 객체가 지속될 때 DireccionEmpresa에 대한 유효성 검사를 트리거합니다. 이렇게하면 중첩 관계에서 JPA/JSF (등)에 의해 모든 제한 예외가 캡처됩니다. 알았어? –
Pablo