I have a spring-boot web application that works with freemarker template engine and uses Twitter bootstrap for styling.
I have an issue with saving entity through submit form.
My entity Category looks like this:
@Entity
@Table(name="category")
public class Category {
@Column(name = "name", unique = true)
@NotNull
private String name;
@ManyToOne
@JoinColumn(name = "parent_category_id")
private Category parentCategory;
@OneToMany(mappedBy = "parentCategory")
private Set<Category> subCategories;}
as you can see it has self reference.
My Controller for saving this entity.
@RequestMapping(value="/create", method= RequestMethod.POST)
public String createCategory(@ModelAttribute("category") Category category, Model model) {
categoryService.save(category);
return "categories";
}
And for creating form for new Category
@RequestMapping("/new")
public String newCategory(Model model) {
List<Category> allCategories = categoryService.getCategories();
model.addAttribute("category", new Category());
model.addAttribute("allCategories", allCategories);
return "category_new";
}
finally in my FTL template file I have (I'l post only part for single select)
<div class="control-group">
<label class="control-label" for="parentCategory">Parent</label>
<div class="controls">
<select id="parentCategory" name="parentCategory">
<option value="">-- None --</option>
<#list allCategories as value>
<option value="${value}">${value.name}</option>
</#list>
</select>
</div>
</div>
Now when new form renders everything is ok. Select options are populated nicely. Like so:
<div class="control-group">
<label class="control-label" for="parentCategory">Parent</label>
<div class="controls">
<select id="parentCategory" name="parentCategory">
<option value="">-- None --</option>
<option value="Entity of type rs.app.web.portal.model.Category with id: 1">Cat 1</option>
<option value="Entity of type rs.app.web.portal.model.Category with id: 2">Cat 2</option>
<option value="Entity of type rs.app.web.portal.model.Category with id: 3">Cat 3</option>
</select>
</div>
</div>
but when I submit form I dont get anything for property parentCategory. There is no error in console. For other properties of Category like 'name' which is String, everything works fine. I get value populated in input. But I cant seem to get selected Parent category ... How do I post Entity as a property on sumbisson form???
Thanks.