Hello, All
I've hit a minor snag in my understanding of ManyToMany relationships with EJB3. In a nutshell, I have a Post Entity which as a ManyToMany relationship with a Keyword Entity. I'm decided to create my own junction table. The Keyword table has a unique key constraint on the keyword field.
The Issue:
I can persist new Post entities with non-existing Keywords entities (e.g. Keywords that don't exist in the database ) But , When I attempt to attach
an existing Keyword entity to a non-persisted Post object the persist fails for the following reason.
org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.web.jList.ejb.entity.Keyword
My understanding:
What i'm expecting is that if i attach an existing Keyword entity to the Post entity then i should have a new row in my Post/Keyword junction table.
I main question is what am i doing wrong?
JBoss 4.0.5GA JEMS install with EJB3 support
My database looks like so:
Post Table idtitledescriptionpostdatelogin_idprohibitedevents_id
1Moving Sale Everything must GO!!!!Test2006-11-16 19:22:02101
2Once's in a life time Sale!!!!Test 22006-11-16 19:22:02102
Post/Keyword Junction Table
idposts_idkeywords_id
113
224
311
416
515
621
Keyword Table
idcountversionkeyword
100car
200tools
300toys
400computers
500babys toys
600car parts
// In my Post Entity @ManyToMany( fetch = FetchType.EAGER , cascade = CascadeType.ALL) @JoinTable( name="keyword_posts_junc", joinColumns={@JoinColumn(name="posts_id")}, inverseJoinColumns={@JoinColumn(name="keywords_id")} ) public Set<Keyword> getKeywords() { return keywords; }
Ok, I've figured out the problem. Although i'm a little confused. It appears that if i call em.merge on my non-presisted Object that has child elements that have been persisted every works as expected. Why is this so?
- Kevin