Adding JSR 303 constraints to persistent fields

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

Flight -> createdDate

Not null

@NotNull

Flight -> createdBy

Not null

@NotNull

Flight -> numOfSeats

Not null

Maximum seats 200

Minimum seats 100

@NotNull

@DecimalMax("200")

@DecimalMin("100")

Flight -> origin

Not null

Maximum length of value of origin is 20, minimum length is 3

@NotNull

@Size(min=3, max=20)

Flight -> destination

Not null

Maximum length of value of destination is 20, minimum length is 3

@NotNull

@Size(min=3, max=20)

FlightKey -> flightId

Not null

@NotNull

FlightKey -> departureDate

Not null

@NotNull

Getting ready

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.

How to do it...

Follow these steps to add JSR 303 constraints:

  1. Create the Flight entity in the sample.roo.flightapp.domain package using the entity command:
    ..roo> entity --class ~.domain.Flight --identifierType ~.domain.FlightKey --table FLIGHT_TBL
    
  2. Add 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
    
  3. Set the Roo prompt on the FlightKey primary key class using the focus command:
    ..roo> focus --class ~.domain.FlightKey
    
  4. Add 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
    

How it works...

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;
    .....
}

There's more...

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.

See also

  • Refer to the Creating persistent entities recipe to see how to create persistent entities and add fields to them
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.61.81