Have to beans:
@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
}
and:
@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
}
In my jsp I'm have form for enter data about book, and multiple list for select author(s) from DB, problem only in select authors, therefore give only this code:
<sf:select multiple="true" path="author" items="${authors}" size="7" >
</sf:select>
Where ${authors} - List with objects Author from DB. Use POST request.
In my controller for this page have this (I know it's not correct):
@RequestMapping(value="/addbook", method=RequestMethod.POST)
public String addBook(Book book){
hibarnateService.saveBook(book);
return "redirect:/books";
}
When I'm create book without select authors, but enter another information, all fine, book save in DB. When select some authors get this - The request sent by the client was syntactically incorrect.
You will need to implement initBinder in your controller, below can be tentative code (not tested)
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(List.class, "authors ", new CustomCollectionEditor(List.class)
{
@Override
protected Object convertElement(Object element)
{
Long id = null;
if(element instanceof Long) {
//From the database 'element' will be a Long
id = (Long) element;
}
return id != null ? authorService.loadAuthorById(id) : null;
}
});
}
Problem solved by add in controller:
@InitBinder
protected void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Author.class, new Editor(hibarnateService));
}
and create class:
public class Editor extends PropertyEditorSupport {
private final Dao hibernateService;
public Editor(Dao hibernateService){
this.hibernateService=hibernateService;
}
@Override
public void setAsText(String text) throws IllegalArgumentException{
Author author=hibernateService.getAuthor(Integer.parseInt(text));
setValue(author);
}
}
P.S. What wrong with me? I can't find the right answer myself until I ask here)
Have to beans:
@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
}
and:
@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
}
In my jsp I'm have form for enter data about book, and multiple list for select author(s) from DB, problem only in select authors, therefore give only this code:
<sf:select multiple="true" path="author" items="${authors}" size="7" >
</sf:select>
Where ${authors} - List with objects Author from DB. Use POST request.
In my controller for this page have this (I know it's not correct):
@RequestMapping(value="/addbook", method=RequestMethod.POST)
public String addBook(Book book){
hibarnateService.saveBook(book);
return "redirect:/books";
}
When I'm create book without select authors, but enter another information, all fine, book save in DB. When select some authors get this - The request sent by the client was syntactically incorrect.
You will need to implement initBinder in your controller, below can be tentative code (not tested)
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(List.class, "authors ", new CustomCollectionEditor(List.class)
{
@Override
protected Object convertElement(Object element)
{
Long id = null;
if(element instanceof Long) {
//From the database 'element' will be a Long
id = (Long) element;
}
return id != null ? authorService.loadAuthorById(id) : null;
}
});
}
Problem solved by add in controller:
@InitBinder
protected void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Author.class, new Editor(hibarnateService));
}
and create class:
public class Editor extends PropertyEditorSupport {
private final Dao hibernateService;
public Editor(Dao hibernateService){
this.hibernateService=hibernateService;
}
@Override
public void setAsText(String text) throws IllegalArgumentException{
Author author=hibernateService.getAuthor(Integer.parseInt(text));
setValue(author);
}
}
P.S. What wrong with me? I can't find the right answer myself until I ask here)
0 commentaires:
Enregistrer un commentaire