Bean Validation support

Another feature introduced in JPA 2.0 is support for JSR 303 Bean Validation. Bean Validation support allows us to annotate our JPA entities with Bean Validation annotations. These annotations allow us to easily validate user input and perform data sanitation.

Taking advantage of Bean Validation is very simple; all we need to do is annotate our JPA Entity fields or getter methods with any of the validation annotations defined in the javax.validation.constraints package. Once our fields are annotated as appropriate, the EntityManager will prevent non-validating data from being persisted.

The following code example is a modified version of the Customer JPA entity we saw earlier in this chapter. It has been modified to take advantage of Bean Validation in some of its fields:

net.ensode.javaee8book.beanvalidation.entity; 
 
import java.io.Serializable; 
 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 
 
@Entity 
@Table(name = "CUSTOMERS") 
public class Customer implements Serializable 
{ 
  @Id 
  @Column(name = "CUSTOMER_ID") 
  private Long customerId; 
 
  @Column(name = "FIRST_NAME") 
  @NotNull  @Size(min=2, max=20) 
  private String firstName; 
 
  @Column(name = "LAST_NAME") 
  @NotNull  @Size(min=2, max=20) 
  private String lastName; 
 
  private String email; 
 
  public Long getCustomerId() 
  { 
    return customerId; 
  } 
 
  public void setCustomerId(Long customerId) 
  { 
    this.customerId = customerId; 
  } 
 
  public String getEmail() 
  { 
    return email; 
  } 
 
  public void setEmail(String email) 
  { 
    this.email = email; 
  } 
 
  public String getFirstName() 
  { 
    return firstName; 
  } 
 
  public void setFirstName(String firstName) 
  { 
    this.firstName = firstName; 
  } 
 
  public String getLastName() 
  { 
    return lastName; 
  } 
 
  public void setLastName(String lastName) 
  { 
    this.lastName = lastName; 
  } 
} 

In this example, we used the @NotNull annotation to prevent the firstName and lastName of our entity from being persisted with null values. We also used the @Size annotation to restrict the minimum and maximum length of these fields.

That is all we need to do to take advantage of Bean Validation in JPA. If our code attempts to persist or update an instance of our entity that does not pass the declared validation, an exception of type javax.validation.ConstraintViolationException will be thrown, and the entity will not be persisted.

As we can see, Bean Validation pretty much automates data validation, freeing us from having to manually write validation code.

In addition to the two annotations discussed in the previous example, the javax.validation.constraints package contains several additional annotations we can use to automate validation on our JPA entities. Please refer to the Java EE 8 API documentation at https://javaee.github.io/javaee-spec/javadocs/ for the complete list.

..................Content has been hidden....................

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