title: Unable to Save Form Data Using Spring and Hibernate
body: <p>I have gone through this: <a href="https://stackoverflow.com/questions/44200018/unable-to-save-form-data-to-database-in-spring-mvc">Unable to save form data to database in spring mvc</a> but did not solve what my issue is. Now, i created two entities. One(Expensegroup) is to be used as a dropdown for the other(Expenses). But anytime I tried to save, it generate this error:</p>
  
  <pre><code>   Field error in object 'expense' on field 'expenseGroup': rejected value [3]; codes [typeMismatch.expense.expenseGroup,typeMismatch.expenseGroup,typeMismatch.com.pebnic.financemgr.entity.ExpenseGroup,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [expense.expenseGroup,expenseGroup]; arguments []; default message [expenseGroup]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.pebnic.financemgr.entity.ExpenseGroup' for property 'expenseGroup'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.pebnic.financemgr.entity.ExpenseGroup' for property 'expenseGroup': no matching editors or conversion strategy found]].
  </code></pre>
  
  <p><strong><em>See the Entity- ExpenseGroup:</em></strong></p>
  
  <pre><code>@Entity
  @Table(name="tab_expgroup")
  public class ExpenseGroup {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="id")
      private int id;
  
      @Column(name="code")
      @NotNull(message="Expense Group Code is required")
      @Size(min=3,message="Expense Group Code must be minimum of 3 Characters")
      private String code;
  
      @Column(name="name")
      @NotNull(message="Expense Group Name is Required")
      private String name;
  
      @Column(name="flag")
      private String flag;
  </code></pre>
  
  <p>getters &amp; Setters</p>
  
  <p><strong>Expense Entity</strong></p>
  
  <pre><code>@Entity
  @Table(name="tab_expenses")
  public class Expense {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="id")
      private int id;
  
      @Column(name="item")
      private String item;
  
      @Column(name="expense_date")
      private String expenseDate;
  
      @Column(name="amount")
      private double amount;
  
      @Column(name="narration")
      private String narration;
  
      //Foreign Key Mapping
      @OneToOne(cascade=CascadeType.ALL)
      @JoinColumn(name="expensegroup_id")
      private ExpenseGroup expenseGroup;
   Getters &amp; setters
  
  
  **Controller:**
  
  @GetMapping("/showForm")
  
  public String showFormAdd(Model theModel) {
  
  Expense theExpense  = new Expense();
  
  List&lt;ExpenseGroup&gt; theExpenseGroup  = expenseService.getExpenseGroupList();
  
  theModel.addAttribute("expense", theExpense);
  
  theModel.addAttribute("expenseGroups", theExpenseGroup);
  
  return "expense-form";
  
  }
  
  
  @PostMapping("/saveExpense")
  
  public String saveExpense(@ModelAttribute("expense")Expense theExpense) {
  
  expenseService.saveExpense(theExpense);
  
  return "redirect:/expense/list";
  
  }
  </code></pre>
  
  <p><strong>DAOImplementation:</strong></p>
  
  <pre><code>@Override
  
  public void saveExpense(Expense theExpense) {
  
  Session currentSession = sessionFactory.getCurrentSession();
  
  currentSession.saveOrUpdate(theExpense);
  
  }
  </code></pre>
  
  <p><strong>See the Form:</strong></p>
  
  <pre><code>form:select path="expenseGroup" class="form-control"&gt;
  
  &lt;form:option value=""&gt;Select the Category&lt;/form:option&gt;
  
  &lt;c:forEach  items="${expenseGroups}" var="grp"&gt;
  
  &lt;form:option value="${grp.id}"&gt;${grp.name}&lt;/form:option&gt;
  
  &lt;/c:forEach&gt;
  
  &lt;/form:select&gt;
  </code></pre>
  
  <p>This is the output of the form above:</p>
  
  <pre><code>&lt;select id="expenseGroup" name="expenseGroup" class="form-control"&gt;
  &lt;option value=""&gt;Select the Category&lt;/option&gt;
  
  &lt;option value="1"&gt;Beverages&lt;/option&gt;
  
  &lt;option value="3"&gt;Cooking&lt;/option&gt;
  
  &lt;option value="6"&gt;Self Development&lt;/option&gt;
  
  &lt;option value="2"&gt;Grains&lt;/option&gt;
  
  &lt;option value="5"&gt;Personal Allowance&lt;/option&gt;
  
  &lt;option value="15"&gt;Self Training&lt;/option&gt;
  
  &lt;option value="4"&gt;Utility&lt;/option&gt;
  
  &lt;/select&gt;
  </code></pre>
  
  <p>Issue: Anytime I try to save, it throws the error of rejected value,no matter what options I select.How can i resolve this,so to save/persist the data, knowing that i am persisting the data using Hibernate Query Language of calling saveorUpdate function.I am new to Spring and hibernate.</p>