title: Implement validations in setters or use Hibernate Validator
body: <p>I am facing a design decision in my GWT + Hibernate project.</p>
  
  <p>I will explain my question using a Company entity.</p>
  
  <pre><code>@Entity
  @Table(name = "Company")
  public class Company extends LightEntity implements BaseSerializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "Id")
      private int id;
  
      @Column(name = "Code")
      private String code;
      @Column(name = "Name")
      private String name;
  
      @Column(name = "Address")
      private String address;
  
      @Column(name = "ContactNumber1")
      private String contactNumber1;
  
      @Column(name = "ContactNumber2")
      private String contactNumber2;
  
      @Column(name = "EMail")
      private String email;
  
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "company", cascade = CascadeType.ALL)
      private List&lt;CompanyRegistration&gt; companyRegistrations = new ArrayList&lt;CompanyRegistration&gt;();
  
      public Company() {
      }
      public Company(String code, String name) {
          this.setCode(code);
          this.setName(name);
      }
  
      // getters &amp; setters
  }
  </code></pre>
  
  <p>A valid Company object must always have a valid code and name. Rest of the properties are optional. Hence, I have provided a two-args constructor to ensure the creation of a valid object.</p>
  
  <p>Now.</p>
  
  <p>I am used to validate data in setters. I also use these setters in my constructor(s). These ensure that a new object always has a valid state. This is/was my plan in implementing the Hibernate entities.</p>
  
  <p>However, GWT-RPC mandates default noargs constructor so that it can be used for deferred binding. But that will also allow anyone to create an empty object, which is not desirable in most of my Hibernate entities. For example, a Company must always be constructed with a Code and a Name. A noargs constructor defies that rule.</p>
  
  <p>Now, Hibernate has implemented a standard (JSR-303, if my memory doesn't betray me) for validation framework. It lets you invoke validations on an already created object.</p>
  
  <p>My problem has arisen due to the noargs constructor. If it had not been mandatory, I would have done away with validations in setter.</p>
  
  <p>If I implement validations in setters, that means that an invalid object will fail as fast as possible. I won't need to invoke a validation on an object on client side. But, an empty object (created using the noargs constructor) can't be validated that way. It might make it way to the service, implying that I also have to implement validations, a lot of which might be the same as the setters, in the service layer.</p>
  
  <p>If I implement the validation framework, then I have to invoke validation on each object I create on the client side. If I forget to validate one, it can go to service layer in an invalid state. However, I can easily use the same framework on the service layer.</p>
  
  <p>One solution that I have thought of is to implement validations in setters anyway, but also use a Validator in the service layer, before sending an object to persist in database. That way, the client side will be restricted by the setters automatically. On the other hand, if an empty object is sent anyhow to the server side, it will be caught by the Validator on service layer. An exception will be thrown to the user in both the cases. </p>
  
  <p>I think this is not the best solution, however. So, I would like to get one.</p>
  
  <p>EDIT - I am anyways using annotations to put constraints on entity fields, so that Hibernate can use them for validation before persisting an object.</p>