title: Thymeleaf: get an object from form
body: <p>Let's say I have an entity:</p>
  
  <pre><code>@Entity
  @Table(name = "Building")
  @Data
  public class Building {
  
  @Id
  @Column(name = "buildingId")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer buildingId;
  
  @OneToOne(cascade = {CascadeType.MERGE}
  @JoinColumn(name = "buildingType")
  private BuildingType buildingType;
   //...
  
  }
  </code></pre>
  
  <p>In my controller I put all building types into the model:</p>
  
  <pre><code>@RequestMapping("/addBuilding")
      public String newBuilding(Model model, @ModelAttribute Building building){
         model.addAttribute("buildingTypes", buildingTypeService.getBuildingTypes()); //gives me an ArrayList&lt;BuildingType&gt;
  }
  </code></pre>
  
  <p>Now I want to iterate over the building types to put them in drop down <code>&lt;select&gt;</code>:</p>
  
  <pre><code>&lt;form class="some-form" method="post" th:action="@{/addBuilding}"&gt;
      &lt;select id="type" class="some-class" required&gt;
          &lt;option th:each="buildingType : ${buildingTypes}" th:text="${buildingType.name}"&gt;&lt;/option&gt;
      &lt;/select&gt;
      &lt;button type="submit" class="button"&gt;Store&lt;/butto&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This works fine. But when I try to get the chosen building type:</p>
  
  <pre><code>&lt;form class="some-form" method="post" th:action="@{/addBuilding}" th:object="${building}"&gt;
          &lt;select id="type" class="some-class" th:field="*{buildingType}" required&gt;
              &lt;option th:each="buildingType : ${buildingTypes}" th:value="${buildingType}" th:text="${buildingType.name}"&gt;&lt;/option&gt;
          &lt;/select&gt;
          &lt;button type="submit" class="button"&gt;Store&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>My controller does not receive an object but a String:</p>
  
  <pre><code>@RequestMapping("/addBuilding")
          public String newBuilding(Model model, @ModelAttribute Building building){
             //...
             System.out.println((building.getBuildingType() instanceof BuildingType)); //false
      }
  </code></pre>
  
  <p>Is it possible to get an object from Thymeleaf ? Or is it neccessary to create a primitive transient field (e.g. String) for BuildingType, return it from Thymeleaf and search in controller for BuildingType based on this field ?</p>