title: Hibernate - List sequence number auto-generation without gaps and starting from zero
body: <p>I have UserObject which contains a list of SearchLog objects. This search log list size is defined to be at max 5.<br>
  Now whenever I add a search log to the UserObject in SearchLog list I want it to be added in the first position. The SearchLog object has a sequence number which I want to be automatically generated by hibernate. So if the searchLog list contains 3 elements on adding 4th searchLog in first position, the sequence number of previously persistent objects should automatically change.</p>
  
  <p>While trying to save the UserObject with a new SearchLog object in the searchLog list, I get the exception that the sequence number of Search log should be specified (its not null field in DB). If I set the sequence number explicitly it works fine, but what I want is that hibernate should automatically handle this.Can this be done? Or is setting sequence number manually the only way.</p>
  
  <p>Here's what I have been trying-</p>
  
  <pre><code>public class UserObject {
    private Integer id;
    private String firstName;
    private List&lt;SearchLog&gt; searchLogs = new ArrayList&lt;SearchLog&gt;();
  }
  
  public class SearchLog {
      private Integer id;
      private String searchQuery;
      private Integer sequenceNumber;
  private Integer userObjectId;
  }  
  </code></pre>
  
  <p>And the corresponding hbm files-<br>
  UserObject.hbm.xml
      
      </p>
  
  <pre><code>&lt;hibernate-mapping package="com.xxx.admin.bo"&gt;
    &lt;class name="UserObject " table="USER" dynamic-update="true" dynamic-insert="true" discriminator-value="AT"&gt;
      &lt;id name="id" type="java.lang.Integer" column="ID" &gt;
        &lt;generator class="identity"/&gt;
      &lt;/id&gt;
      &lt;property name="firstName" type="java.lang.String" column="FIRST_NAME" length="30"/&gt;
      &lt;list name="searchLogs" table="SEARCH_LOG" cascade="all-delete-orphan" lazy="true" inverse="true" &gt;
        &lt;key column="USER_OBJECT_ID" /&gt;
        &lt;index column="SEQUENCE_NUMBER" /&gt;
        &lt;one-to-many class="SearchLog" /&gt;    
      &lt;/list&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>SearchLog.hbm.xml
      
      </p>
  
  <pre><code>&lt;hibernate-mapping package="com.xxx.admin.bo"&gt;
    &lt;class name="SearchLog" table="SEARCH_LOG" dynamic-update="true" dynamic-insert="true" discriminator-value="AT"&gt;
      &lt;id name="id" type="java.lang.Integer" column="ID"&gt;
        &lt;generator class="identity"/&gt;
      &lt;/id&gt;
      &lt;property name="searchQuery" type="java.lang.String" column="SEARCH_QUERY" length="250"/&gt;
      &lt;property name="sequenceNumber" type="java.lang.Integer" column="SEQUENCE_NUMBER" not-null="true" /&gt;
      &lt;property name="userObjectId" type="java.lang.Integer" column="USER_OBJECT_ID"/&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Code block of save method</p>
  
  <pre><code>userObject.getSearchLogs().add(0, searchLog);
  getUserDao().save(userObject);
  </code></pre>