title: Using 'select' tag with entities in Thymeleaf
body: <p>I am creating a form with the select tag that looks like this:</p>
  
  <pre><code>&lt;form th:object="${version}" method="post" class="form-horizontal"&gt;
      ...
      &lt;div class="control-group" th:classappend="${#fields.hasErrors('product')} ? 'error'"&gt;
          &lt;label class="control-label" for="product" th:text="#{version.product}"&gt;Product&lt;/label&gt;
          &lt;div class="controls"&gt;
              &lt;select id="product" th:field="*{product}"&gt;
                  &lt;option value="" th:text="#{common.select.prompt}"&gt;&lt;/option&gt;
                  &lt;option th:each="p : ${productList}" th:value="${p.id}"  th:text="${p.name}"&gt;&lt;/option&gt;
              &lt;/select&gt;
              &lt;span class="help-inline" th:errors="*{product}"&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      ...
  &lt;/form&gt;
  </code></pre>
  
  <p><code>DomainClassConverter</code> class of <code>Spring Data JPA</code> helps to auto-convert selected <code>id</code> to the entity <code>Product</code> when I submit the form. The <code>product</code> should also be not null (I am using <code>@NotNull</code> on the <code>product</code> field in the <code>Version</code> class.</p>
  
  <p>The problem that I have - when I come back to edit the data, the <code>Product</code> is not selected.</p>
  
  <p>If I modify the <code>select</code> like this (<code>th:field</code> and <code>th:errors</code>): <code>&lt;-- p.s. is not a sad smile</code></p>
  
  <pre><code>&lt;select id="product" th:field="*{product.id}"&gt;
      &lt;option value="" th:text="#{common.select.prompt}"&gt;&lt;/option&gt;
      &lt;option th:each="p : ${productList}" th:value="${p.id}" th:text="${p.name}"&gt;&lt;/option&gt;
  &lt;/select&gt;
  &lt;span class="help-inline" th:errors="*{product.id}"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>then it becomes selected when I come back to edit it, but the validator doesn't work (<code>product</code> is always instantiated, even if selected id is <code>null</code>).</p>
  
  <p>It looks like a very common scenario (selecting an entity from the list), but I cannot find any good looking example. Please share the secret knowledge.</p>