首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JSP页为forein键参数发送空值

JSP页为forein键参数发送空值
EN

Stack Overflow用户
提问于 2016-08-24 15:37:07
回答 1查看 64关注 0票数 0

我有两个表,Employee和City .Employee表有一个指向城市的外键(CityID)。我试图从下拉列表中获取CityID值,但从jsp页面返回的模型的CityID值始终为null值;

员工实体

代码语言:javascript
运行
复制
@Entity
@Table(name = "Employee", catalog = "DataGathering", schema = "dbo")
...
@JoinColumn(name = "CityID", referencedColumnName = "ID")
@ManyToOne
private City CityID;

城市实体

代码语言:javascript
运行
复制
@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表单

代码语言:javascript
运行
复制
<td><form:select path="CityID" >
 <form:options items = "${cityList}" itemValue="id"itemLabel="cityName"/>
</form:select></td>

控制器

代码语言:javascript
运行
复制
@RequestMapping(value="/new", method = RequestMethod.POST)
public String 
processRegistration(@ModelAttribute("tblEmployee")TblEmployee employee,
BindingResult result,
                Map<String, Object> model) {

    employeeService.create(employee);

    return "employee";
}

错误:

代码语言:javascript
运行
复制
Cannot insert the value NULL into column 'CityID', table 
'DataGathering.dbo.Employee'; column does not allow nulls. INSERT fails.
EN

Stack Overflow用户

发布于 2016-08-24 15:44:28

我不确定,但一旦更改了下面的代码

代码语言:javascript
运行
复制
<td><form:select path="CityID" >
<form:options items = "${cityList}" itemValue="id"itemLabel="cityName"/>
</form:select></td>

作为,

代码语言:javascript
运行
复制
<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页面中获取值,则将实体更改为

代码语言:javascript
运行
复制
fetch = FetchType.EAGER
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39117146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档