title: How to handle foreign keys relation in JOOQ
body: <p>I have to create a new record of an entity with some given properties. </p>
  
  <p>Will be more like coping an existing record with some values replaced using <strong><code>JOOQ</code></strong>. I can do it for a single table, but I am finding right way to do it for related entities. </p>
  
  <p>For example: The main table is Student and I need a new records of student of grade 10. I can fetch a records of student in grade 10 and change name and save it. </p>
  
  <pre><code>    Result&lt;Record&gt; result= context.select().from(Student.STUDENT).where(Student.STUDENT.GRADE.eq(studRecords.getGrade()))
                  .fetch();
          StudentRecord appER=(StudentRecord ) result.get(0);
              Field&lt;?&gt;[] fields= Student.STUDENT.fields();
  for (Field&lt;?&gt; field : fields)
              {
                  //System.out.println(field.getName() + " = " +field.getType() + field.getDataType() + "  " + field.getBinding());
                  DataType&lt;Comparable&gt; dt= (DataType&lt;Comparable&gt;) field.getDataType() ;
                //if(!dt.getSQLDataType().nullable()  &amp;&amp; !dt.getSQLDataType().defaulted() &amp;&amp; studRecords.getValue(field) ==null)
                  if(studRecords.getValue(field) ==null)
                {  
                    if(studRecords.getTable().getPrimaryKey().getFields().contains(field)){
                        System.out.println("Primary key is null . New Record Adding");
                        continue;
                    }
                    if(t.getTable().getReferences().contains(field))
                        System.out.println("Foreign key");
                    Object obj=appER.getValue(field);
                      if(field.getType().equals(Integer.class))
                       {
                        studRecords.setValue((Field&lt;Comparable&gt;)field,(obj!=null?(Integer)obj :new Integer(0)));
                       }
                        if(field.getType().equals(Boolean.class)){
                            studRecords.setValue((Field&lt;Comparable&gt;)field,(obj!=null?(Boolean)obj :new Boolean(false)));
                        }
                        if(field.getType().equals(Byte.class)){
                            studRecords.setValue((Field&lt;Comparable&gt;)field,(obj!=null?(Byte)obj :new Byte((byte) 0)));
                        }
                        if(field.getType().getName().equals("java.lang.String")){
                            studRecords.setValue((Field&lt;Comparable&gt;)field,(obj!=null?(String)obj :new String("")));
                        }
                        if(field.getType().equals(Double.class)){
                            studRecords.setValue((Field&lt;Comparable&gt;)field,(obj!=null?(Double)obj :new Double(0.0)));
                        }
                        if(field.getType().equals(Long.class)){
                            studRecords.setValue((Field&lt;Comparable&gt;)field,(obj!=null?(Long)obj :new Long(0)));
                        }
                }
              }
  </code></pre>
  
  <p>But now I also want to create related object like address, hobbies which are related tables.</p>
  
  <p>What would be the best way to do it?</p>