title: Populate a Select dropdown from Query with Spring Thymeleaf
body: <p>I need to populate a dropdown list mapping from a entity (table) attribute for a form. Since the values are static (there's only 6 values repeated along the insert values), i thought that it might be a good idea get a list of those values and send to the Controller class and map into the view as Select button with Thymeleaf.</p>
  <p>I know i could get the attribute 'Status' values directly from the entity, but i dont know the best way to do it</p>
  <p>So, my question is:</p>
  <p>Is it possible to populate a select dropdown button from a jpql query? Is there another better way to populate from unique entity attributes?</p>
  <p>I tried to create a query like the next one, to get unique values from the attribute 'Status' which belongs to a entity named 'Orders', but this query generates a list without an associated id just the unique values (Also I thought about to create a query with a new temporary autoincrement id column, without success).</p>
  <p>This is my entity:</p>
  <pre><code>@Data
  @Entity
  @Table(name=&quot;orders&quot;)
  public class Orders implements Serializable {
      @Id
      private Integer orderNumber;
      
      @Column
      private String orderDate;
      
      @Column
      private String requiredDate;
      
      @Column
      private String shippedDate;
      
      @Column
      private String status;
  // ...
  }
  </code></pre>
  <pre><code>@Repository(&quot;ordersDAO&quot;)
  public interface OrdersDAO extends JpaRepository&lt;Orders,Integer&gt;{
      
      @Query(&quot;SELECT DISTINCT o.status FROM orders o&quot;)
  // 
      @Transactional(readOnly = true)
      public List &lt;Orders&gt; getAllStatus();
  </code></pre>
  <p>From this point forward i can't resolve (neither the Controller nor form templates).</p>
  <p><strong>UPDATE</strong></p>
  <p>I'm trying this code, but it doesn't work. Let me know the errors to improve:</p>
  <p><strong>Service Implementation</strong></p>
  <pre><code>public class OrdersServiceImpl implements OrdersService{
   @Override
      public List&lt;Orders&gt; getAllStatus() {
          return ordersDAO.getAllStatus();
      }
  }
  </code></pre>
  <p><strong>Controller</strong></p>
  <pre><code>@Controller
  @Slf4j
  public class IndexController {
  
      String url = &quot;&quot;;
  
      @Autowired
      @Qualifier(&quot;ordersServiceImpl&quot;)
      private OrdersService ordersService;
      
      @GetMapping(&quot;/&quot;)
      public String getStatusDropdown(Model model){
          
          List&lt;Orders&gt; statusDropdown = ordersService.getAllStatus();
          model.addAttribute(&quot;statusDropdown&quot;, statusDropdown);
          return &quot;forms&quot;;
      }
  </code></pre>
  <p><strong>HTML form</strong></p>
  <pre><code>&lt;div class=&quot;form-row align-items-center&quot;&gt;
     &lt;div th:object=&quot;${statusDropdown}&quot; class=&quot;col-2 my-1&quot;&gt;
        &lt;label for=&quot;Estado&quot; class=&quot;mr-sm-2&quot;&gt;Estado&lt;/label&gt;
        &lt;select class=&quot;custom-select mr-sm-2&quot; id=&quot;Estado&quot; form=&quot;ordersForm&quot;&gt;
        &lt;option selected&gt;Seleccione Estado&lt;/option&gt;
        &lt;option th:each=&quot;status : ${statusDropdown}&quot; th:text=&quot;${status}&quot; th:value=&quot;${status}&quot;&gt;Estado 1&lt;/option&gt;
        &lt;/select&gt;
     &lt;/div&gt;
  
     &lt;div class=&quot;col align-self-end my-1&quot;&gt;
        &lt;input class=&quot;btn btn-info&quot; type=&quot;submit&quot; value=&quot;Buscar&quot; th:href=&quot;@{/getOrdersByStatus}&quot;&gt;    
     &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>