2016-08-24 3 views
0

私は2つのテーブルを持ちます。EmployeeとCity .EmployeeテーブルにはCity(CityID)への外部キーがあります。 私はドロップダウンリストからCityID値を取得しようとしていますが、jspページから返されるモデルは常にCityIDのnull値を持ちます。JSPページがforein鍵パラメータに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"; 
} 

ERROR:

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

回答ありがとうございます。同じerorrで間違っています。 –

+0

jspページで都市リストを取得していますか? –