1 Reply Latest reply on Dec 5, 2006 2:35 PM by fowlks

    Problems with ManyToMany with non-existing child entity inse

      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;
       }