1 package ejava.jpa.example.validation;
2
3 import java.text.DateFormat;
4
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7
8 import javax.persistence.*;
9 import javax.validation.constraints.NotNull;
10 import javax.validation.constraints.Past;
11 import javax.validation.constraints.Pattern;
12 import javax.validation.constraints.Size;
13
14
15
16
17
18 @Entity
19 @Table(name="VALIDATION_PERSON")
20 public class Person {
21 @Id @GeneratedValue
22 private int id;
23
24 @Column(name="FIRST_NAME", length=12, nullable=false)
25
26
27
28 @ValidName(min=1, max=12, regexp="^[a-zA-Z\\ \\-]+$", message="invalid first name")
29 private String firstName;
30
31 @Column(name="LAST_NAME", length=20, nullable=false)
32
33
34
35 @ValidName(min=1, max=20, regexp="^[a-zA-Z\\ \\-]+$", message="invalid last name")
36 private String lastName;
37
38 @Temporal(TemporalType.DATE)
39 @NotNull(groups={Drivers.class, POCs.class})
40 @Past(groups=Drivers.class)
41 @MinAge.List({
42 @MinAge(age=18, groups=POCs.class),
43 @MinAge(age=16, groups=Drivers.class)
44 })
45 private Date birthDate;
46
47 @Column(name="EMAIL", length=50)
48 @NotNull(groups=POCs.class)
49 @Size(min=7,max=50)
50 @Pattern(regexp="^.+@.+\\..+$")
51 private String email;
52
53 public int getId() { return id; }
54
55 public String getFirstName() { return firstName; }
56 public Person setFirstName(String firstName) {
57 this.firstName = firstName;
58 return this;
59 }
60
61 public String getLastName() { return lastName; }
62 public Person setLastName(String lastName) {
63 this.lastName = lastName;
64 return this;
65 }
66
67 public Date getBirthDate() { return birthDate; }
68 public Person setBirthDate(Date birthDate) {
69 this.birthDate = birthDate;
70 return this;
71 }
72
73 public String getEmail() { return email; }
74 public Person setEmail(String email) {
75 this.email = email;
76 return this;
77 }
78
79 @Override
80 public String toString() {
81 DateFormat df = new SimpleDateFormat("YYYY");
82 return (firstName==null ? "null" : firstName) +
83 (lastName==null ? ", null" : ", " + lastName) +
84 (birthDate==null ? ", null dob" : ", " + df.format(birthDate)) +
85 (email==null ? ", null email" : ", " + email)
86 ;
87 }
88 }