我有两个表,Employee和City .Employee表有一个指向城市的外键(CityID)。我试图从下拉列表中获取CityID值,但从jsp页面返回的模型的CityID值始终为null值;
员工实体
@Entity
@Table(name = "Employee", catalog = "DataGathering", schema = "dbo")
...
@JoinColumn(name = "CityID", referencedColumnName = "ID")
@ManyToOne
private City CityID;城市实体
@Id
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "CityName")
private String cityName;
@OneToMany(mappedBy = "CityID")
private Collection<Employee> EmployeeCollection1;jsp表单
<td><form:select path="CityID" >
 <form:options items = "${cityList}" itemValue="id"itemLabel="cityName"/>
</form:select></td>控制器
@RequestMapping(value="/new", method = RequestMethod.POST)
public String 
processRegistration(@ModelAttribute("tblEmployee")TblEmployee employee,
BindingResult result,
                Map<String, Object> model) {
    employeeService.create(employee);
    return "employee";
}错误:
Cannot insert the value NULL into column 'CityID', table 
'DataGathering.dbo.Employee'; column does not allow nulls. INSERT fails.发布于 2016-08-24 15:44:28
我不确定,但一旦更改了下面的代码
<td><form:select path="CityID" >
<form:options items = "${cityList}" itemValue="id"itemLabel="cityName"/>
</form:select></td>作为,
<td><form:select path="CityID" >
<c:forEach item="${cityList}" var="cityList">
   <options value= "${cityList.id}" itemLabel="cityName">${cityList.cityName}</option>
</c:forEach>
</form:select></td>如果没有在jsp页面中获取值,则将实体更改为
fetch = FetchType.EAGERhttps://stackoverflow.com/questions/39117146
复制相似问题