3 Replies Latest reply on May 29, 2007 10:45 AM by torsty

    ManyToMany with EntityManager in flush mode manual

    torsty

      Hi, I have got a question in regard to (bidirectional) ManyToMany relations. I have two classes (Job and Skill) that are linked by ManyToMany Association (Skill has the mappedBy attribute). I executed the following code:

      // insert new Skill (wrapped in transaction)
      Skill hibernate = new Skill( "Hibernate" );
      Skill struts = new Skill( "Struts" );
      this.getEm().persist( hibernate );
      this.getEm().persist( struts );
      
      // insert new Job (separte transaction - skill already commited)
      //--- fetch the already saved Skill
      Skill hibernateSkill = ( Skill ) this.getEm().createQuery( "from Skill skill where skill.name= :name ")
      .setParameter( "name", "Hibernate" )
      .getSingleResult();
      
      //insert new Job and link to skill (wrapped in transaction)
      Job webDeveloper = new Job( "Web Developer");
      webDeveloper.getSkills().add( hibernateSkill );
      hibernateSkill.getJobs().add( webDeveloper );
      this.getEm().persist(webDeveloper);
      



      The insert works fine, when I inject the EntityManager in my Seam conversational Component with @PersistenceContext( type=EXTENDED). The problem occurs when I inject the EntityManager with @In(value="entityManager") and begin the conversation with @Begin(flushMode=FlushModeType.MANUAL ). Then only the Job entity is inserted, but no association to Skill. When updating an already persisted job (just adding a new Skill to the association) everything works fine. The Problem with @PersistenceContext EntityManager is, that the Associations get updated immediaty, when a new Skill is added to a managed Job entity without having to manually persist the job entity. I don't want that behaviour. That's why I would like to use flushMode manual (only works with Seam EntityManager).

      I could use a work around, but can anyone explain to me why no associations are inserted when persisting an unmanaged (i.e. new) job entity with the Seam EntityManager (@In annotation) and flush mode manual?
      Or am I doing something wrong?