JSR 303 (bean validation) defines a standard approach for annotations-based JavaBeans validation. In this recipe we'll look at how Spring Roo's field
command can be used to add JSR 303 validation constraints to persistent fields of entities.
The following table shows the validation constraints that apply to fields defined in the Flight
entity and FlightKey
class of our flight-app
project:
Persistent field |
Constraint |
JSR 303 annotation |
---|---|---|
|
Not null |
|
|
Not null |
|
|
Not null Maximum seats 200 Minimum seats 100 |
|
|
Not null Maximum length of value of |
|
|
Not null Maximum length of value of |
|
|
Not null |
|
|
Not null |
|
Exit the Roo shell and delete the contents of the C:
oo-cookbookch02-recipes
directory.
Execute the ch02_jpa_setup.roo
script. It creates a flight-app
Roo project and sets up Hibernate as a persistence provider using the persistence
setup
command. If you are using a different database than MySQL or your connection settings are different from what is specified in the script, then modify the script accordingly.
Start the Roo shell from the C:
oo-cookbookch02-recipes
directory.
Follow these steps to add JSR 303 constraints:
Flight
entity in the sample.roo.flightapp.domain
package using the entity
command:..roo> entity --class ~.domain.Flight --identifierType ~.domain.FlightKey --table FLIGHT_TBL
numOfSeats
, origin
, destination
, createdBy
, modifiedBy
, createdDate
, and modifiedDate
fields to the Flight
entity, as shown here:..roo> field number --type java.lang.Integer --fieldName numOfSeats --column NUM_OF_SEATS --notNull --decimalMin 100 --decimalMax 200 ..roo> field string --fieldName origin --column FLT_ORIGIN --notNull --sizeMin 3 --sizeMax 20 ..roo> field string --fieldName destination --column FLT_DESTINATION --notNull --sizeMin 3 --sizeMax 20 ..roo> field date --type java.util.Date --fieldName createdDate --column CREATED_DATE --notNull ..roo> field string --fieldName createdBy --column CREATED_BY --notNull
FlightKey
primary key class using the focus
command:..roo> focus --class ~.domain.FlightKey
flightId
and departureDate
fields to the FlightKey
entity, as shown here:..roo> field string --fieldName flightId --column FLIGHT_ID --notNull ..roo> field date --fieldName departureDate --type java.util.Date --notNull --column FLT_DEP_DATE
In the field
command you can use arguments such as notNull
, nullRequired
, decimalMax
, decimalMin
, regexp
, sizeMax
, and sizeMin
to specify the validation constraints that apply to a field. The use of these arguments will result in the generation of fields that are annotated with JSR 303 annotations, as shown here for Flight
entity:
public class Flight { @NotNull @DecimalMin("100") @DecimalMax("200") @Column(name="NUM_OF_SEATS") private Integer numOfSeats; @NotNull @Column(name = "FLT_ORIGIN") @Size(min = 3, max = 20) private String origin; @NotNull @Column(name = "FLT_DESTINATION") @Size(min = 3, max = 20) private String destination; ..... }
Using JSR 303 constraints is not limited to domain objects; you can use JSR 303 constraints in any class, irrespective of the tier in which the class is used. For instance, you can use JSR 303 constraints in command or form-backing objects of your web tier.
18.188.61.81