title: Spring - Failed to convert property value of type java.lang.String to required type
body: <p>I am making a project of the Housing Association in Spring.</p>
  
  <p>When I'm trying to add an object to my list of apartments I'm getting an error that is written somehow on the page:
  <a href="https://s28.postimg.org/vrhy6mbd9/blad.jpg" rel="nofollow noreferrer">https://s28.postimg.org/vrhy6mbd9/blad.jpg</a></p>
  
  <p>Apartments have relation Many to One Building.</p>
  
  <p>Apartment Controller:</p>
  
  <pre><code>package pl.dmcs.spoldzielnia.controllers;
  
  import java.util.Map;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.ServletRequestUtils;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.SessionAttributes;
  import org.springframework.web.servlet.ModelAndView;
  
  import pl.dmcs.spoldzielnia.domain.Apartment;
  import pl.dmcs.spoldzielnia.service.ApartmentService;
  import pl.dmcs.spoldzielnia.service.BuildingService;
  
  
  
  
  @Controller
  @SessionAttributes
  public class ApartmentController {
  
      @Autowired
      ApartmentService apartmentService;
  
  
  
      @Autowired
      BuildingService buildingService;
  
  
  
      @RequestMapping("admin/apartment")
      public String listApartment(Map&lt;String, Object&gt; map, HttpServletRequest request) {
  
          int apartmentId = ServletRequestUtils.getIntParameter(request, "apartmentId" , -1);
  
          if (apartmentId &gt; 0)
          {   
              Apartment apartment = apartmentService.getApartment(apartmentId);
  
              apartment.setBuilding(buildingService.getBuilding(apartmentService.getApartment(apartmentId).getBuilding().getId()));
  
  
              map.put("selectedBuilding", apartmentService.getApartment(apartmentId).getBuilding().getId());
              map.put("apartment", apartment);
          }
          else
             map.put("apartment", new Apartment());   
  
  
          map.put("buildingList", buildingService.listBuilding());
          map.put("apartmentList", apartmentService.listApartment());
  
          return "apartment";
      }
  
     @RequestMapping(value = "admin/addApartment", method = RequestMethod.POST)
     public String addContact(@ModelAttribute("apartment") Apartment apartment, BindingResult result, 
             HttpServletRequest request, Map&lt;String, Object&gt; map) {
  
  
  
         if (result.getErrorCount()==0)
         {
             if (apartment.getId()==0)
             {
  
                 if (apartment.getBuilding().getId() &gt; 0)
                     apartment.setBuilding(buildingService.getBuilding(apartment.getBuilding().getId()));
                 apartmentService.addApartment(apartment);
             }
             else
             {
  
                 apartmentService.editApartment(apartment);
             }
  
  
             return "redirect:/admin/apartment.html";
         }
  
  
         map.put("buildingList", buildingService.listBuilding());
         map.put("apartmentList", apartmentService.listApartment());
         return "apartment";
     }
  
     @RequestMapping("admin/delete/apartment/{apartmentId}")
     public String deleteApartment(@PathVariable("apartmentId") Integer apartmentId) {
         apartmentService.removeApartment(apartmentId);
  
         return "redirect:/admin/apartment.html";
     }
  //   @RequestMapping("/apartment")
  //   public ModelAndView showContacts() {
  //        
  //       return new ModelAndView("apartment", "command", new Apartment());
  //   }
  </code></pre>
  
  <p>Domain:</p>
  
  <pre><code>  package pl.dmcs.spoldzielnia.domain;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  
  @Entity
  @Table(name="apartment")
  public class Apartment {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      int id;
      @Column(name="apartmentNumber", nullable=false)
      private String number;
  
      public String getNumber() {
          return number;
      }
      public void setNumber(String number) {
          this.number = number;
      }
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
  
  
  
  
      @ManyToOne
      private Building building;
  
      public Building getBuilding() {
          return building;
      }
      public void setBuilding(Building building) {
          this.building = building;
      }
  }
  
  
  }
  </code></pre>
  
  <p>Building Service Implementation:</p>
  
  <pre><code>package pl.dmcs.spoldzielnia.service;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import pl.dmcs.spoldzielnia.dao.BuildingDAO;
  import pl.dmcs.spoldzielnia.domain.Building;
  import pl.dmcs.spoldzielnia.domain.Building;
  
  @Service
  @Transactional
  public class BuildingServiceImpl implements BuildingService{
  
      @Autowired
      BuildingDAO buildingDAO;
  
      @Transactional
      public void addBuilding(Building building) {
  
  
          buildingDAO.addBuilding(building);
      }
  
      @Transactional
      public List&lt;Building&gt; listBuilding() {
  
          return buildingDAO.listBuilding();
      }
  
      @Transactional
      public Building getBuilding(int id) {
          return buildingDAO.getBuilding(id);
      }
  
      @Transactional
      public void removeBuilding(int id) {
          buildingDAO.removeBuilding(id);
      }
  
      @Transactional
      public void editBuilding(Building building) {
  
          buildingDAO.editBuilding(building);
      }
  }
  </code></pre>
  
  <p>Could you help me to solve my problem?</p>