title: How to avoid bidirectional insertion on ManyToMany relationship
body: <p>I have two tables with a m:n relationship. This relationship should be bidirectional when I am extracting the data from the database, so, I need the <code>@ManyToMany</code> in both entity classes. But, at the same time, I need that the non owner class does not insert the owner class when I perform a persist or merge operation on it. </p>
  
  <p>For example, I have the <code>Gene</code> class, that is the owner class, and I have the <code>Ontology</code> class. One Gene has many Ontologies and one Ontology has many Genes. Classical many-to-many relationship. If I persist or merge a Gene I want to insert its Ontologies too, but I don't want that this Ontology insertion insert all other Genes linked to it.</p>
  
  <p>On other hand, if I insert an Ontology I don't want to insert the genes linked to that Ontology.</p>
  
  <p>I have been trying with a lot of JPA tags on the <code>@ManyToMany</code> and nothing works on the way that I want.</p>
  
  <p>Have any one an Idea to solve this problem?</p>
  
  <p>The Gene class</p>
  
  <pre><code>@ManyToMany(cascade = CascadeType.ALL)
  @JoinTable(name = GeneTables.GENEINFO_HAS_ONTOLOGY,
  joinColumns =
  @JoinColumn(name = "GeneInfo_WID", referencedColumnName = "WID"),
  inverseJoinColumns =
  @JoinColumn(name = "Ontology_WID", referencedColumnName = "WID"))
  private Set&lt;Ontology&gt; ontology;
  </code></pre>
  
  <p>The Ontology class</p>
  
  <pre><code>@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "ontology")
  private Set&lt;GeneInfo&gt; geneInfo;
  </code></pre>
  
  <p>I tried all the Cascade types and even without the cascade option. The result is the same.</p>
  
  <p>I have this error when I execute this code</p>
  
  <pre><code>EntityManager em = getEntityManager();
  m.getTransaction().begin();
  em.persist(ontology);
  em.getTransaction().commit();
  </code></pre>
  
  <p>I have this error:</p>
  
  <pre><code>[EL Warning]: 2012-08-29 14:52:13.013--UnitOfWork(544628019)--java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST 
  
  at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
  
  at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
  
  at org.jbiowh.core.datasets.ontology.controller.OntologyJpaController.create(OntologyJpaController.java:41)
  
  at org.jbiowh.tools.prototypes.Test.main(Test.java:65)
  </code></pre>
  
  <p><strong>Solved</strong> </p>
  
  <p>I solved the problem. I create controller classes to handle the special cascades.</p>
  
  <p>The create method on the Ontology controller class will be:</p>
  
  <pre><code>if (loadGeneFlag &amp;&amp; ontology.getGene() != null &amp;&amp; !ontology.getGene().isEmpty()) {
      Set&lt;Gene&gt; geneSet = new HashSet&lt;&gt;();
      GeneJpaController gController = new GeneJpaController(emf);
      for (Gene gene : ontology.getGene()) {
          Gene geneOnDB = em.find(Gene.class, gene.getWid());
          if (geneOnDB != null) {
              geneSet.add(geneOnDB);
          } else {
              gController.create(gene);
              geneSet.add(em.getReference(Gene.class, gene.getWid()));
          }
      }
      ontology.setGene(geneSet);
  }
  </code></pre>
  
  <p>This code will create all gene references using the Gene controller class and not following the cascade operation. This give me the possibility to handle the Gene cascades correctly on the Gene controller class. Now, I don't have any duplicate object neither exceptions.</p>