2016-08-24 34 views
0

我有2個表,員工和城市.Employee表具有City(CityID)的外鍵。 我試圖從下拉列表中獲取CityID值,但從jsp頁面返回的模型始終具有CityID的空值;JSP頁面爲forein關鍵參數發送空值

僱員實體

@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. 

回答

0

我不知道,但一旦改變婁代碼

<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.EAGER 
+0

感謝您的迴應,但它沒有工作t與同一個erorr發生錯誤。 –

+0

你在jsp頁面獲得城市列表嗎? –