I have mapped two entities with OneToMany through an HashMap. It seems to work rather good, except one thing. When Im adding a new HashMap to my Author and then try to update the values will be presisted except for the joincolumn in the booktable. Its always null. No exception is thrown.
Author
Code:
@Entity
public class Author {
private Long id;
private String name;
private Map <String, Book> bookList = new HashMap <String, Book> ();
[...]
@OneToMany(mappedBy="author",targetEntity=Book.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@MapKey(name="id")
public Map<String,Book> getBookList() {
return bookList;
}
}
BookCode:
public class Book {
private String id;
private String name;
private Long authorId;
private author;
[...]
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="author_id",referencedColumnName="id")
public Author getAuthor() {
return author;
}
}
jspCode:
[...]
bookList.put(new Book("Foo","Bar"));
Author author = authorService.getAuthorById(1L);
author.setBookList(bookList);
authorService.updateAuthor(author);
Q: Why is the author_id in book table never set.
Q: The old posts remains untouched i the table. Replacing an collection with objects should replace all post in the table(some thing like "delete from Book where author_id = ..." )
Enviroment:
Java 1.6 / 6
Mysql 5.x
jboss-4.0.5.GA
Thanks!