title: ORMLite many to many relation Android
body: <p>I have two classes :</p>
  
  <p>UniteStratigraphique.java :</p>
  
  <pre><code>    @DatabaseTable(tableName = "unitestratigraphique")
      public class UniteStratigraphique {
      public final static String ID_FIELD_NAME = "id";
          @DatabaseField(generatedId = true, columnName = ID_FIELD_NAME)
          private int id;
      // CAMPAGNES
          @DatabaseField(foreign = true, foreignAutoRefresh = true)
          private Campagne campagne;
          @ForeignCollectionField
          private ForeignCollection&lt;Campagne&gt; listeCampagnes;
  
  public UniteStratigraphique() {}
  
      public Campagne getCampagne() {
              return campagne;
          }
  
          public void setCampagne(Campagne campagne) {
              this.campagne = campagne;
          }
  
          public ArrayList&lt;Campagne&gt; getListeCampagnes() {
              ArrayList&lt;Campagne&gt; campagnesArray = new ArrayList&lt;Campagne&gt;();
              for (Campagne campagne : listeCampagnes) {
                  campagnesArray.add(campagne);
              }
              return campagnesArray;
          }
  
          public ForeignCollection&lt;Campagne&gt; getListeCampagnesForeign() {
              return listeCampagnes;
          }
  
          public void setListeCampagnes(ForeignCollection&lt;Campagne&gt; listeCampagnes) {
              this.listeCampagnes = listeCampagnes;
          }
  
      }
  </code></pre>
  
  <p>Campagne.java :</p>
  
  <pre><code>@DatabaseTable(tableName = "campagne")
  public class Campagne {
  @DatabaseField(generatedId = true)
      private int id;
  // UNITE STRATIGRAPHIQUE
      @ForeignCollectionField
      private ForeignCollection&lt;UniteStratigraphique&gt; listeUniteStratigraphique;
      @DatabaseField(foreign = true, foreignAutoRefresh = true)
      private UniteStratigraphique uniteStratigraphique;
  public Campagne() {}
  
  public ArrayList&lt;UniteStratigraphique&gt; getListeUniteStratigraphique() {
          ArrayList&lt;UniteStratigraphique&gt; usArray = new ArrayList&lt;UniteStratigraphique&gt;();
          for (UniteStratigraphique us : listeUniteStratigraphique){
              usArray.add(us);
          }
          return usArray;
      }
  
      public ForeignCollection&lt;UniteStratigraphique&gt; getListeUniteStratigraphiqueForeign() {
          return listeUniteStratigraphique;
      }
  
      public void setListeUniteStratigraphique(
              ForeignCollection&lt;UniteStratigraphique&gt; listeUniteStratigraphique) {
          this.listeUniteStratigraphique = listeUniteStratigraphique;
      }
  
      public int getSizeListeUniteStratigraphique() {
          return listeUniteStratigraphique.size();
      }
  
      public UniteStratigraphique getUniteStratigraphique() {
          return uniteStratigraphique;
      }
  
      public void setUniteStratigraphique(UniteStratigraphique uniteStratigraphique) {
          this.uniteStratigraphique = uniteStratigraphique;
      }
  }
  </code></pre>
  
  <p>As you can see, these are Many-To-Many linked (0...n---0...n, with ORMLite annotations).</p>
  
  <p>Now, my workflow is :</p>
  
  <hr>
  
  <p>I create multiple "UniteStratigraphique" classes and I store them into my database (this works fine).</p>
  
  <p>=> So I have n * "UniteStratigraphique" stored.</p>
  
  <p>After that what I want is to create a "Campagne" class wich will contain multiple "UniteStratigraphique" classes. </p>
  
  <p>=> So I want to set this field from "Campagne.java" :</p>
  
  <pre><code> @ForeignCollectionField
      private ForeignCollection&lt;UniteStratigraphique&gt; listeUniteStratigraphique;
  </code></pre>
  
  <p>with the n * "UniteStratigraphique" elements I just stored before.</p>
  
  <p>I tried to do this with this DAO method but it only duplicate the "UniteStratigraphique" classes into my db and no link is made..</p>
  
  <pre><code> public void addUsToCampagne(Campagne campagne,
              ArrayList&lt;UniteStratigraphique&gt; usArray) {
          ForeignCollection&lt;UniteStratigraphique&gt; usForeign = campagne
                  .getListeUniteStratigraphiqueForeign();
          if (usForeign == null) {
              try {
                  usForeign = getHelper().getCampagneDao()
                          .getEmptyForeignCollection("listeUniteStratigraphique");
  
                  for (UniteStratigraphique us : usArray) {
                          usForeign.add(us);
                  }
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }else{
              for (UniteStratigraphique us : usArray) {
                      usForeign.add(us);
              }
          }
  
      }
  </code></pre>
  
  <p>And in my Activity I'm doing this :</p>
  
  <pre><code>db.addCampagne(campagne);
  
              if( myUniteStratigraphiqueArray.size() &gt; 0){
                      db.addUsToCampagne(campagne, myUniteStratigraphiqueArray);
              }
  </code></pre>