title: How to properly test Hibernate length restriction?
body: <p>I have a POJO mapped with Hibernate for persistence. In my mapping I specify the following:</p>
  
  <pre><code>&lt;class name="ExpertiseArea"&gt;
      &lt;id name="id" type="string"&gt;
          &lt;generator class="assigned" /&gt;
      &lt;/id&gt;
      &lt;version name="version" column="VERSION" unsaved-value="null" /&gt;
      &lt;property name="name" type="string" unique="true" not-null="true" length="100" /&gt;
      ...
  &lt;/class&gt;
  </code></pre>
  
  <p>And I want to test that if I set a name longer than 100 characters, the change won't be persisted. I have a DAO where I save the entity with the following code:</p>
  
  <pre><code>public T makePersistent(T entity){
      transaction = getSession().beginTransaction();
      transaction.begin();
  
      try{
          getSession().saveOrUpdate(entity);
          transaction.commit();
      }catch(HibernateException e){
          logger.debug(e.getMessage());
          transaction.rollback();
      }   
      return entity;
  }
  </code></pre>
  
  <p>Actually the code above is from a GenericDAO which all my DAOs inherit from. Then I created the following test:</p>
  
  <pre><code>public void testNameLengthMustBe100orLess(){
      ExpertiseArea ea = new ExpertiseArea(
          "1234567890" +
          "1234567890" +
          "1234567890" +
          "1234567890" +
          "1234567890" +
          "1234567890" +
          "1234567890" +
          "1234567890" +
          "1234567890" +
          "1234567890");
  
      assertTrue("Name should be 100 characters long", ea.getName().length() == 100);
  
      ead.makePersistent(ea);
      List&lt;ExpertiseArea&gt; result = ead.findAll();
      assertEquals("Size must be 1", result.size(),1);
  
      ea.setName(ea.getName()+"1234567890");
      ead.makePersistent(ea);
  
      ExpertiseArea retrieved = ead.findById(ea.getId(), false);
      assertTrue("Both objects should be equal", retrieved.equals(ea));
      assertTrue("Name should be 100 characters long", (retrieved.getName().length() == 100));
  }
  </code></pre>
  
  <p>The object is persisted ok. Then I set a name longer than 100 characters and try to save the changes, which fails:</p>
  
  <pre><code>14:12:14,608  INFO StringType:162 - could not bind value '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890' to parameter: 2; data exception: string data, right truncation
  14:12:14,611  WARN JDBCExceptionReporter:100 - SQL Error: -3401, SQLState: 22001
  14:12:14,611 ERROR JDBCExceptionReporter:101 - data exception: string data, right truncation
  14:12:14,614 ERROR AbstractFlushingEventListener:324 - Could not synchronize database state with session
  org.hibernate.exception.DataException: could not update: [com.exp.model.ExpertiseArea#33BA7E09-3A79-4C9D-888B-4263314076AF]
  
  //Stack trace
  
  14:12:14,615 DEBUG GenericDAO:87 - could not update: [com.exp.model.ExpertiseArea#33BA7E09-3A79-4C9D-888B-4263314076AF]
  14:12:14,616 DEBUG JDBCTransaction:186 - rollback
  14:12:14,616 DEBUG JDBCTransaction:197 - rolled back JDBC Connection
  </code></pre>
  
  <p>That's expected behavior. However when I retrieve the persisted object to check if its name is still 100 characters long, the test fails. </p>
  
  <p>The way I see it, the retrieved object should have a name that is 100 characters long, given that the attempted update failed. The last assertion fails because the name is 110 characters long now, as if the <code>ea</code> instance was indeed updated.</p>
  
  <p>What am I doing wrong here?</p>