View Javadoc
1   package ejava.jpa.example.validation;
2   
3   import javax.persistence.*;
4   import javax.validation.constraints.NotNull;
5   import javax.validation.constraints.Pattern;
6   import javax.validation.constraints.Size;
7   
8   /**
9    * This class provides an example of using GroupSequences where you can 
10   * organize validation groups into a sequence which will short circuit
11   * once one of the groups fails.
12   */
13  @Entity
14  @Table(name="VALIDATION_ADDRESS")
15  @CityStateOrZip(groups=PreCheck.class)
16  public class Address1 {
17  	@Id @GeneratedValue
18  	private int id;
19  		
20  	@Column(name="STREET", length=32, nullable=false)
21  	@NotNull(message="street not supplied")
22  	@Size(max=32, message="street name too large", groups=DBChecks.class)
23  	@Pattern(regexp="^[0-9A-Za-z\\ ]+$", groups=DataChecks.class, 
24  	         message="street must be numbers and letters")
25  	private String street;
26  	
27  	@Column(name="CITY", length=20, nullable=false)
28  	@NotNull(message="city not supplied")
29  	@Size(max=20, message="city name too large", groups=DBChecks.class)
30  	@Pattern(regexp="^[a-zA-Z\\ ]+$", groups=DataChecks.class, 
31  	         message="city must be upper and lower case characters")
32  	private String city;
33  	
34  	@Column(name="STATE", length=2, nullable=false)
35  	@NotNull(message="state not supplied")
36  	@Size(min=2, max=2, message="state wrong size", groups=DBChecks.class)
37  	@Pattern(regexp="^[A-Z][A-Z]$", groups=DataChecks.class, 
38  	         message="state must be upper case letters")
39  	private String state;
40  	
41  	@Column(name="ZIP", length=5, nullable=false)
42  	@NotNull(message="zipcode not supplied")
43  	@Size(min=5, max=5, message="zipcode wrong size", groups=DBChecks.class)
44  	@Pattern(regexp="^[0-9][0-9][0-9][0-9][0-9]$", groups=DataChecks.class, 
45  	         message="zipcode must be numeric digits")
46  	private String zip;
47  
48  	
49  	public int getId() { return id; }
50  
51  	public String getStreet() { return street; }
52  	public Address1 setStreet(String street) {
53  		this.street = street;
54  		return this;
55  	}
56  
57  	public String getCity() { return city; }
58  	public Address1 setCity(String city) {
59  		this.city = city;
60  		return this;
61  	}
62  
63  	public String getState() { return state; }
64  	public Address1 setState(String state) {
65  		this.state = state;
66  		return this;
67  	}
68  
69  	public String getZip() { return zip; }
70  	public Address1 setZip(String zip) {
71  		this.zip = zip;
72  		return this;
73  	}
74  	
75  	@Override
76  	public String toString() {
77  		return (street==null?"null":street) + " " + 
78  	           (city==null?"null":city) + ", " + 
79  			   (state==null?"null":state) + " " + 
80  	           (zip==null?"null":zip);
81  	}
82  	
83  }