There is a News class that has a list of Category objects (inherits id from the BasedEntity class):
@Entity @Table(name = "categories") public class Category extends BasedEntity { @Column(name = "name") private String name; public Category() { } public Category(int id, String name) { this.setId(id); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } News class:
@Entity @Table(name = "news") public class News extends BasedEntity { @Column(name = "headline") private String headline; @Column(name = "content") private String content; @Column(name = "date") private Date date; @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinTable(name = "news_categories", joinColumns = {@JoinColumn(name = "news_id")}, inverseJoinColumns = {@JoinColumn(name = "category_id")}) private Set<Category> categories=new HashSet<>(); The form:
<form:select path="categories" multiple="true"> <form:option value="1">Политика</form:option> <form:option value="2">Экономика</form:option> <form:option value="3">Спорт</form:option> </form:select> Controller:
@RequestMapping(value = "save", method = RequestMethod.POST, headers = "content-type=application/x-www-form-urlencoded", consumes = "application/json;charset=UTF-8") public void saveNews(News news, BindingResult result){ newsService.save(news); } The problem is that the controller does not connect select with a lot of categories.