title: Collections in ORMLite
body: <p>Hello 
  I want persist some collections data with ORMlite in my android app. For example : </p>
  
  <pre><code>class Person {
    @DatabaseField(generatedId=true)
    private int id;
  
    @DatabaseField
    private String name;
  
    @DatabaseField
    private String surname;
  
    @ForeignCollectionField
    private Collections&lt;Phone&gt; phones;
  }
  
  class Phone {
    @DatabaseField(generatedId=true)
    private int id;
  
    @DatabaseField
    private String number;
  
    @DatabaseField
    private String type; 
  }
  </code></pre>
  
  <p>How i should do it to persist this collection. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I use ForeignCollectionField as you told me. But now i have another question - is possible construction like that (I changed class a little) </p>
  
  <pre><code>@DatabaseTable
  public class Student {
  
      @DatabaseField(generatedId=true)
      private int id;
  
      @DatabaseField
      private String name;
  
      @DatabaseField
      private String surname;
  
      @DatabaseField
      private String semestr;
  
      @ForeignCollectionField
      private Collection&lt;Adres&gt; adres;
  }
  
  public class Adres implements Serializable {
  
      @DatabaseField(generatedId=true)
      private int id;
  
      @DatabaseField(foreign=true, columnName="student_id")
      private Student student;
  
      @DatabaseField
      private String miasto;
  
      @DatabaseField
      private String ulica;
  
      @DatabaseField
      private String nr;
  
      @ForeignCollectionField
      private Collection&lt;Phone&gt; phone;
  }
  
  public class Phone {
  
      @DatabaseField(generatedId=true)
      private int id;
  
      @DatabaseField
      private String number;
  
      @DatabaseField(foreign=true, columnName="adres_id" )
      private Adres adres;
  }
  </code></pre>
  
  <p>I build this structure, i have it in 3 tabelas. </p>
  
  <pre><code>Student queryForStudentMarcin = simpleDao.queryForId(studentMarcin.getId());
  Collection&lt;Adres&gt; adres = queryForStudentMarcin.getAdres();
  
  for (Adres a : adres) {
      int id = a.getId();
      String miasto = a.getMiasto();
      String ulica = a.getUlica();
      String nr = a.getNr();
  }
  </code></pre>
  
  <p>Then i do this and error appear:</p>
  
  <pre><code>04-03 15:55:58.392: ERROR/AndroidRuntime(6506): FATAL EXCEPTION: main
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506): java.lang.NullPointerException
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.j256.ormlite.field.FieldType.buildForeignCollection(FieldType.java:579)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:58)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:107)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.j256.ormlite.stmt.SelectIterator.next(SelectIterator.java:120)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.example.orm.MainActivity.dodajDoBazyCollection(MainActivity.java:140)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.example.orm.MainActivity$2.onClick(MainActivity.java:66)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.view.View.performClick(View.java:2408)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.view.View$PerformClick.run(View.java:8816)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.os.Handler.handleCallback(Handler.java:587)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.os.Handler.dispatchMessage(Handler.java:92)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.os.Looper.loop(Looper.java:123)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.app.ActivityThread.main(ActivityThread.java:4627)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at java.lang.reflect.Method.invokeNative(Native Method)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at java.lang.reflect.Method.invoke(Method.java:521)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Any suggestions ? </p>
  
  <p><strong>Edit2</strong></p>
  
  <p>Problem was read data from db. This procedure work ok, it's now nice, but i just use it to understand ORMlite. Thx very much Gray for help. </p>
  
  <pre><code>protected void czytajZBazy() throws SQLException {
  
      Dao&lt;Student, Integer&gt; simpleDao = getHelper().getSimpleDao();
      Dao&lt;Adres, Integer&gt; adresDao = getHelper().getAdresDao();
      Dao&lt;Phone, Integer&gt; phoneDao = getHelper().getPhoneDao();
  
      /**
       * wyswietl adresy dla studenta Marcin
       */
      QueryBuilder&lt;Student, Integer&gt; qbStudent = simpleDao.queryBuilder();;
      Where&lt;Student, Integer&gt; where = qbStudent.where();
      where.eq("name", "Marcin");
      PreparedQuery&lt;Student&gt; prepare = qbStudent.prepare();
      CloseableIterator&lt;Student&gt; iterator = simpleDao.iterator(prepare);
      while(iterator.hasNext()){
          Log.v("inside ", "loop");
          Student next = iterator.next();
          Log.v("sudent", ""+next.getId()+" "+next.getName()+" "+next.getSurname());
  
          QueryBuilder&lt;Adres, Integer&gt; adresQB = adresDao.queryBuilder();
          Where&lt;Adres, Integer&gt; where2 = adresQB.where();
  
          where2.eq("student_id", next.getId());
  
          PreparedQuery&lt;Adres&gt; preparedAdres = adresQB.prepare();
          CloseableIterator&lt;Adres&gt; iterator2 = adresDao.iterator(preparedAdres);
  
          while(iterator2.hasNext()){
              Log.v("iterator po adresie", "!!");
              Adres nextAdres = iterator2.next();
              Log.v("Adres", ""+nextAdres.getId()+" "+nextAdres.getMiasto());
              //wypisz telefony 
              QueryBuilder&lt;Phone, Integer&gt; queryPhone = phoneDao.queryBuilder();
              Where&lt;Phone, Integer&gt; where3 = queryPhone.where();
              where3.eq("adres_id", nextAdres.getId());
  
              PreparedQuery&lt;Phone&gt; preparedPhone = queryPhone.prepare();
              CloseableIterator&lt;Phone&gt; iterator3 = phoneDao.iterator(preparedPhone);
  
              while(iterator3.hasNext()){
                  Log.v("inside phone iterator", "");
                  Phone next2 = iterator3.next();
                  Log.v("phone", ""+next2.getId()+" "+next2.getNumber());
              }
              iterator3.close();
          }
          iterator2.close();
      }
      iterator.close();
  }
  </code></pre>