title: How to validate Entity classes with a JUnit test - Hibernate @Column annotation
body: <p>I have a set of Entity Classes, which are generated by Hibernate tools. All have @Column annotations like:</p>
  
  <pre><code>@Column(name = "CNTR_DESCRIPTION", nullable = false, length = 5)
  public String getDescription() {
      return this.description;
  }
  </code></pre>
  
  <p>I want to write a JUnit test to validate my input to the database, but validation with JUnit only works when adding:</p>
  
  <pre><code>@NotNull
  @Size(max = 5)
  @Column(name = "CNTR_DESCRIPTION", nullable = false, length = 5)
  public String getDescription() {
      return this.description;
  }
  </code></pre>
  
  <p>I prefer NOT to add any annotations, since then I need to change the automatic generated Entity Classes.
  How Can I get the JUnit test working with the first generated @Column annotations?
  Thanks!</p>
  
  <p>My JUnit test (does not work with only @Column, but does work with the extra @NotNull and @Size):</p>
  
  <p>public class CountryEntityTest {
      private static Validator validator;</p>
  
  <pre><code>@BeforeClass
  public static void setUp() {
      ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
      validator = factory.getValidator();
  }
  
  @Test
  public void countryDescriptionIsNull() {
      CountryEntity country = new CountryEntity();
      country.setDescription(null);
      Set&lt;ConstraintViolation&lt;CountryEntity&gt;&gt; constraintViolations = validator.validate( country );
      assertEquals( 1, constraintViolations.size() );
      assertEquals( "may not be null", constraintViolations.iterator().next().getMessage() );
  }
  
  @Test
  public void countryDescriptionSize() {      
  
      CountryEntity country = new CountryEntity();
      country.setDescription("To long");
  
      Set&lt;ConstraintViolation&lt;CountryEntity&gt;&gt; constraintViolations = validator.validate( country );
  
      assertEquals( 1, constraintViolations.size() );
      assertEquals( "size must be between 0 and 5", constraintViolations.iterator().next().getMessage());
  }
  </code></pre>
  
  <p>}</p>