我是hibernate&的新手。 spring,我想知道如何使用hibernate验证器来映射实体类,例如:如果我有以下实体类,它们之间有一对一的关系。如何使用hibernate验证器的@NotNull等注释来验证两个实体的以下字段:" personName,personPhone,personEmail,addressLine1,addressLine2,city,state,& zipCode"。
@Entity
@Table ( name = "person" )
public class FacilityModel {
@Id
@GeneratedValue ( strategy = GenerationType.IDENTITY )
@Column ( name = "pers_id" )
private int persId;
@Column ( name = "pers_name" )
private String personName;
@OneToOne ( cascade = CascadeType.ALL )
@JoinColumn ( name = "pers_Addr_Id" )
private AddressModel address;
@Column ( name = "pers_phone_no" )
private String personPhone;
@Column ( name = "pers_email" )
private String personEmail;
和
@Entity
@Table ( name = "address" )
public class AddressModel {
@Id
@GeneratedValue ( strategy = GenerationType.IDENTITY )
@Column ( name = "addr_id" )
private int addrId;
@Column ( name = "addr_address_line1" )
private String addrLine1;
@Column ( name = "addr_address_line2" )
private String addrLine2;
@Column ( name = "addr_city" )
private String city;
@Column ( name = "addr_state" )
private String state;
@Column ( name = "addr_country" )
private String country;
@Column ( name = "addr_zip_code" )
private String zipCode;
此外,我是否需要在控制器中包含除@Valid注释和BindingResult之外的任何其他代码。
请任何人帮助我。
答案 0 :(得分:0)
您只需要在字段上添加要验证的注释,然后在保存之前休眠将检查您的值是否正确。
http://hibernate.org/validator/
如果在控制器中添加注释@Valid,您还可以看到您拥有的对象无法保存。