Comments on: EJB 3.0 Persistence – ManyToMany relations or the objectification of the intersection table Friends of Oracle and Java Sat, 01 Aug 2015 11:10:05 +0000 hourly 1 By: Peter Sat, 07 Jun 2008 08:00:43 +0000 The refresh() exception is not a contradiction to the specification. Per the specification, the entity passed to the merge() method becomes unmanaged, and the method returns a new T entity that IS managed. For the example, the following code would be successful:

jsf = libraryService.getEntityManager().merge(jsf);

Make sure you don’t lose managed objects through the merge() operation — or many methods could throw the same exception refresh() did.


By: mario kofler Thu, 13 Dec 2007 14:10:23 +0000 hello jim,

i believe he is doing this because he wants to have the possibility to delete the data in the join_table starting from both entities (author and book). if one would be mapped by the “mappedBy” parameter starting the deletion from this entity would not be possible because it is not the “owning” side of the relation.

By: Jim McCollom Tue, 23 Oct 2007 15:43:08 +0000 Great article. I was wondering why you mapped both of your classes with the @JoinTable annotation, instead of using the mappedBy parameter.

@JoinTable(table = @Table(name = “als_authorships”), joinColumns = {
@JoinColumn(name = “bok_id”)
}, inverseJoinColumns = {
@JoinColumn(name = “atr_id”)
public Collection getAuthors() {
return this.authors;

And in

@ManyToMany(mappedBy=”authors”, cascade=CascadeType.ALL)
public Collection getBooks() {
return this.books;

By: jeroen dijkmeijer Wed, 03 Oct 2007 13:35:10 +0000 Good article,
but i’m not sure whether the whole many-to-many mapping option in jpa is the right approach for use case like this, read my blog for some information and 2 short examples on how to implement it.

By: Iner Tue, 02 Oct 2007 20:21:26 +0000 Hello,

Thanks for the article.
Having a bit of trouble though, you fill the tables with data from your als_data.sql.
Could you provide some code on how a fill up the book and the author, does the ‘ALS_AUTHORSHIPS’-table
get filled in some ‘magical’ way ? I mean we do not have an Entity-class for that table.
So how would I write the correct code for inserting the books – would like to do that from a small web-page.

seen some examples out there, but they are full with errors.

hope that you can provide som help.

regards, Iner

By: Thomas Sun, 23 Sep 2007 13:02:01 +0000 Good article :) I’m also wondering how to get the intersection table attribute Contributor Type.
An Author has contributed to many books (stored in the Collection books), but it doesn’t store if the author wrote the whole book or drew the illustrations. What is the best way to solve that?

By: Roger Allen Thu, 15 Feb 2007 18:37:06 +0000 I was just wondering how you would get to the intersection table attribute Contributor Type.

By: Lucas Jellema Mon, 16 Oct 2006 08:01:37 +0000 Good point. I was a bit sloppy when coding the example. Thanks for the correction.


By: Chris Thu, 22 Jun 2006 18:00:30 +0000 Great writeup. One thing I’d like to mention though is the merge/refresh functionality. Refresh is only to be used on Managed entities, where as merge is used on detatched entities and will return the managed instance with the changes merged into it. So to avoid the exception, your code really should be:

managedJsf = libraryService.getEntityManager().merge(jsf);
libraryService.getEntityManager().refresh(managedJsf );

Though it might be better just to call find and then refresh on the returned entity.

Best Regards

By: John Fri, 14 Apr 2006 02:43:41 +0000 Very good articles you wrote. Thank you very much for sharing.

By: Lucas Jellema Mon, 02 Jan 2006 16:11:44 +0000 The ER Diagram in this article was created using Oracle Designer. The Server diagram showing the tables was done with Oracle JDeveloper. Thanks for your kind words!



By: Marcello Mon, 02 Jan 2006 11:11:07 +0000 I am reading with interest your EJB3 articles. Thanks!
Which tool are you using to create the ER diagrams present in this article?