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