我经历过这个:Unable to save form data to database in spring mvc但没有解决我的问题。现在,我创建了两个实体。一个(费用组)将用作另一个(费用)的下拉列表。但每当我尝试保存时,它都会生成此错误:
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]].
查看实体 - ExpenseGroup:
@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;
getter 和 setter
费用实体
@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 & setters
**Controller:**
@GetMapping("/showForm")
public String showFormAdd(Model theModel) {
Expense theExpense = new Expense();
List<ExpenseGroup> 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";
}
DAO实现:
@Override
public void saveExpense(Expense theExpense) {
Session currentSession = sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(theExpense);
}
查看表格:
form:select path="expenseGroup" class="form-control">
<form:option value="">Select the Category</form:option>
<c:forEach items="${expenseGroups}" var="grp">
<form:option value="${grp.id}">${grp.name}</form:option>
</c:forEach>
</form:select>
这是上面表单的输出:
<select id="expenseGroup" name="expenseGroup" class="form-control">
<option value="">Select the Category</option>
<option value="1">Beverages</option>
<option value="3">Cooking</option>
<option value="6">Self Development</option>
<option value="2">Grains</option>
<option value="5">Personal Allowance</option>
<option value="15">Self Training</option>
<option value="4">Utility</option>
</select>
问题:每当我尝试保存时,无论我选择什么选项,它都会抛出拒绝值的错误。我如何解决这个问题,以便保存/保存数据,知道我正在使用 Hibernate 查询保存数据调用 saveorUpdate 函数的语言。我是 Spring 和 hibernate 的新手。
最佳答案
在费用模型中使用一对一(mappedBy),并在费用组模型中使用与连接列名称相同的名称。
关于java - 无法使用 Spring 和 Hibernate 保存表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58604588/