title: Hibernate session gets flushed when using hibernate validation
body: <p>I am using JSR-303 validation in hibernate. I have created a custom validator annotation which checks for data integrity by querying the database. </p>
  
  <p><strong>For example:</strong></p>
  
  <pre><code>public boolean isValid(Object value, ConstraintValidatorContext context) {
      if(value == null){
          return true;
      }
      //This method uses entityManager to fetch a list from database
      Map&lt;String, String&gt; pickList = pickListProvider.getPickList(picklistName);
      return pickList.contains(value);
  }
  </code></pre>
  
  <p><strong>Usage:</strong></p>
  
  <pre><code>public class UserProfile extends Persistent {
      //Member fields come here
  
      @PicklistConstraint(name="languages")
      private String prefLanguage;
  }
  </code></pre>
  
  <p>Here UserProfile is an entity persisted using Hibernate. Hibernate calls this validation at pre insert or pre update. However when the validator tries to fetch records from the database, hibernate is flushing the session. Since the domain object on which validation runs is not fully baked (doesn't have Id field), I get the following exception</p>
  
  <pre><code>org.hibernate.AssertionFailure: null id in &lt;domain object here&gt; entry (don't flush the Session after an exception occurs)
      at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:79)
      at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:194)
      at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:156)
      at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:225)
      at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
      at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:58)
      at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1186)
      at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1241)
      at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
      at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:257)
  </code></pre>
  
  <p>Upon investigation I found that flushMightBeNeeded of DefaultAutoFlushEventListener returns true:</p>
  
  <pre><code>private boolean flushMightBeNeeded(final EventSource source) {
      return !source.getFlushMode().lessThan(FlushMode.AUTO) &amp;&amp;
              source.getDontFlushFromFind() == 0 &amp;&amp;
              ( source.getPersistenceContext().getEntityEntries().size() &gt; 0 ||
                      source.getPersistenceContext().getCollectionEntries().size() &gt; 0 );
  }
  </code></pre>
  
  <p>The issue is that FlushMode is AUTO and source.getPersistenceContext().getEntityEntries().size() has some entries.
  What should be my approach in such a scenario? Should I change the FlushMode from AUTO to MANUAL? Is it possible to get an new session or persistent context, different from the actual entity?
  I am using spring mvc along with hibernate.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I found that source.getPersistenceContext().getEntityEntries() has the entity which is currently being validated. Should that be present here before getting inserted?</p>