我有一个问题要问。尝试将新属性保存到数据库中时出现此错误。列“property_id”不能为空。尝试将要素列表保存到数据库时发生异常。由于某种原因,插入的属性与特征之间没有关系。Property_id 在对象 PropertyFeature 中不存在。
这是我的数据:
table 'property':
id
name
table 'features'
id
title
property_id [not null]
实体属性
@Table(name = "property")
public class Property extends...{
@Column(name = "name", nullable = true)
private String name;
@OneToMany(mappedBy = "property", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<PropertyFeature> features;
public Property() {
}
public Property(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<PropertyFeature> getFeatures() {
return features;
}
public void setFeatures(Set<PropertyFeature> features) {
this.features = features;
}
}
实体属性特征
@Entity
@Table(name = "property_feature")
public class PropertyFeature extends ... {
@Column(name = "title", nullable = false)
private String title;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Property property;
public PropertyFeature() {
}
public PropertyFeature(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Property getProperty() {
return property;
}
public void setProperty(Property property) {
this.property = property;
}
}
弹簧控制器
@Controller
@RequestMapping(value="/account")
public class AccountController {
@Autowired
PropertyServiceImpl propertyServiceImpl;
@RequestMapping(method = RequestMethod.GET, value = "/test")
public String accountPage(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
Property property = propertyServiceImpl.load(12L);
ArrayList<PropertyFeature> test = new ArrayList<PropertyFeature>();
test.add(new PropertyFeature("Feature 1"));
test.add(new PropertyFeature("Feature 2"));
test.add(new PropertyFeature("Feature 3"));
model.put("property", property);
model.put("testList", test);
return "/test";
}
@RequestMapping(method = RequestMethod.POST, value = "/test")
public String accountPost(HttpServletRequest request, HttpServletResponse response, Property property) {
propertyServiceImpl.update(property);
return "/test";
}
}
JSP 格式:
<form:form method="post" commandName="property">
Name <form:input path="name" type="text" name="name" value="" />
Features
<form:select multiple="true" path="features">
<form:options items="${testList}" itemValue="title" itemLabel="title"/>
</form:select>
<input type="submit" name="submit" value="Test" />