【发布时间】:2014-04-04 07:44:07
【问题描述】:
不得不豆:
@Entity
@Table(name="book")
public class Book {
@Id
@Column(name="id_book")
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
private int id;
@Column
@Size(min=1,max=100)
private String title;
@Column
@Size(min=1,max=400)
private String description;
@Column
private Integer year=0;
@ManyToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER)
@Fetch (FetchMode.SELECT)
@JoinTable(name="book_author",
joinColumns={@JoinColumn(name="book_id_book")},
inverseJoinColumns= {@JoinColumn(name="author_id_author")})
private List<Author> author=new ArrayList<Author>();
//getters/setters
}
和:
@Entity
@Table(name="author")
public class Author {
@Id
@Column(name="id_author")
@GeneratedValue
private Integer id;
@Column
private String name;
@Column
private String surname;
@ManyToMany(mappedBy="author")
private Set<Book> book=new HashSet<Book>();
//getters/setters
}
在我的 jsp 中,我有用于输入书籍数据的表单,以及用于从 DB 中选择作者的多个列表,仅在选择作者中存在问题,因此仅提供以下代码:
<sf:select multiple="true" path="author" items="${authors}" size="7" >
</sf:select>
Where ${authors} - 包含来自数据库的作者对象的列表。使用 POST 请求。
在我的这个页面的控制器中有这个(我知道这是不正确的):
@RequestMapping(value="/addbook", method=RequestMethod.POST)
public String addBook(Book book){
hibarnateService.saveBook(book);
return "redirect:/books";
}
当我在没有选择作者的情况下创建书籍时,但输入其他信息,一切都很好,书籍保存在数据库中。 When select some authors get this - The request sent by the client was syntactically incorrect.
【问题讨论】:
标签: java spring spring-mvc model-view-controller collections