Hi!
I have a BookBean with a @ManyToMany relationship to a AuthorBean (entity beans)
in BookBean:
private Collection author;
@ManyToMany(fetch=FetchType.EAGER)
@JoinColumn(name="Author_ID")
public Collection getAuthor() { return this.author; }
public void setAuthor(Collection author) { this.author = author; }
and in AuthorBean:
private Collection books;
@ManyToMany(fetch=FetchType.EAGER)
@JoinColumn(name="Book_ID")
public Collection getBooks() { return this.books; }
public void setBooks(Collection books) { this.books = books; }
I read the statement (I think it was from Bill Burke) that CMR in EJB 3.0 is totally different
from EJB 2.x and that you have to fill the collections as you would have done with regular
POJOs. Another statement I read was that the Many side "owns" the relationship. With these
two statements I created the following addAuthor method:
public int addAuthor(String firstname, String lastname, int[] liste) { AuthorEntityBean author = new AuthorEntityBean(firstname, lastname); Collection<BookEntityBean> books = new ArrayList<BookEntityBean>(); for (int i = 0; i < liste.length; ++i) { BookEntityBean be = em.find(BuchEntityBean.class, Integer.valueOf(liste\[i\])); // be.getAuthors().add(author); books.add(be); } author.setBooks(books); em.persist(author); return author.getId(); }