I have a question to ask. I am getting this error while trying to save a new property into the database. Column ‘property_id’ cannot be null. An exception happens when tryint to save features list into the database. For some reason there is not relation between the property that was inserted and features. Property_id does not exist inside the object PropertyFeature.
Here are my data:
table 'property':
id
name
table 'features'
id
title
property_id [not null]
Entity Property
@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 PropertyFeature
@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;
}
}
SPRING CONTROLLER
@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: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" />
Hibernate needs a foreign key on property_feature to join back to the
ManyToOnemapped property. You have not provided a column through the use of@JoinColumntherefore Hibernate has created one in your schema using its defaulting mechanism. This is described here in section 2.2.5.3.1.4.You have mapped Property as being non-optional, therefore Hibernate expects that you always set a
Propertyon yourPropertyFeaturebefore saving. You have added thePropertyFeatureinto the list on Property but you have not set thePropertyonPropertyFeature.I would suggest adding a method on to your property class as follows:
then in your controller you can write: