Java 如何处理JOOQ中的外键关系

Java 如何处理JOOQ中的外键关系,java,jooq,Java,Jooq,我必须创建具有某些给定属性的实体的新记录 将更像是使用JOOQ替换某些值来处理现有记录。我可以为单个表执行此操作,但我正在为相关实体找到正确的方法 例如:主表是Student,我需要10年级学生的新记录。我可以取一份十年级学生的记录,然后改名保存 Result<Record> result= context.select().from(Student.STUDENT).where(Student.STUDENT.GRADE.eq(studRecords.getGrade())

我必须创建具有某些给定属性的实体的新记录

将更像是使用JOOQ替换某些值来处理现有记录。我可以为单个表执行此操作,但我正在为相关实体找到正确的方法

例如:主表是Student,我需要10年级学生的新记录。我可以取一份十年级学生的记录,然后改名保存

    Result<Record> result= context.select().from(Student.STUDENT).where(Student.STUDENT.GRADE.eq(studRecords.getGrade()))
                .fetch();
        StudentRecord appER=(StudentRecord ) result.get(0);
            Field<?>[] fields= Student.STUDENT.fields();
for (Field<?> field : fields)
            {
                //System.out.println(field.getName() + " = " +field.getType() + field.getDataType() + "  " + field.getBinding());
                DataType<Comparable> dt= (DataType<Comparable>) field.getDataType() ;
              //if(!dt.getSQLDataType().nullable()  && !dt.getSQLDataType().defaulted() && 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<Comparable>)field,(obj!=null?(Integer)obj :new Integer(0)));
                     }
                      if(field.getType().equals(Boolean.class)){
                          studRecords.setValue((Field<Comparable>)field,(obj!=null?(Boolean)obj :new Boolean(false)));
                      }
                      if(field.getType().equals(Byte.class)){
                          studRecords.setValue((Field<Comparable>)field,(obj!=null?(Byte)obj :new Byte((byte) 0)));
                      }
                      if(field.getType().getName().equals("java.lang.String")){
                          studRecords.setValue((Field<Comparable>)field,(obj!=null?(String)obj :new String("")));
                      }
                      if(field.getType().equals(Double.class)){
                          studRecords.setValue((Field<Comparable>)field,(obj!=null?(Double)obj :new Double(0.0)));
                      }
                      if(field.getType().equals(Long.class)){
                          studRecords.setValue((Field<Comparable>)field,(obj!=null?(Long)obj :new Long(0)));
                      }
              }
            }
Result-Result=context.select().from(Student.Student).where(Student.Student.GRADE.eq(studRecords.getGrade()))
.fetch();
StudentRecord appER=(StudentRecord)result.get(0);
字段[]字段=Student.Student.fields();
用于(字段:字段)
{
//System.out.println(field.getName()+“=”+field.getType()+field.getDataType()+“”+field.getBinding());
DataType dt=(DataType)字段。getDataType();
//如果(!dt.getSQLDataType().nullable()&&&!dt.getSQLDataType().defaulted()&&studRecords.getValue(字段)==null)
if(studRecords.getValue(字段)==null)
{  
if(studRecords.getTable().getPrimaryKey().getFields().contains(字段)){
System.out.println(“主键为空,添加新记录”);
继续;
}
if(t.getTable().getReferences()包含(字段))
System.out.println(“外键”);
对象对象=appER.getValue(字段);
if(field.getType().equals(Integer.class))
{
studRecords.setValue((字段)字段,(obj!=null?(整数)obj:新整数(0));
}
if(field.getType().equals(Boolean.class)){
studRecords.setValue((字段)字段,(obj!=null?(布尔)obj:new Boolean(false));
}
if(field.getType().equals(Byte.class)){
studRecords.setValue((字段)字段,(obj!=null?(字节)obj:新字节((字节)0));
}
if(field.getType().getName().equals(“java.lang.String”)){
studRecords.setValue((字段)字段,(obj!=null?(字符串)obj:新字符串(“”));
}
if(field.getType().equals(Double.class)){
studRecords.setValue((字段)字段,(obj!=null?(Double)obj:newdouble(0.0));
}
if(field.getType().equals(Long.class)){
studRecords.setValue((字段)字段,(obj!=null?(Long)obj:new Long(0));
}
}
}
但是现在我还想创建相关的对象,比如地址,嗜好,它们是相关的表


最好的方法是什么?

如果我理解正确,您的大多数代码示例看起来都不必要。要更新学生姓名,您可以执行以下操作:

final List students=context.selectFrom(Student.Student)
.其中(学生.学生.成绩.情商(10))
.fetchInto(StudentRecord.class);
学生(期末成绩记录学生:学生){
student.setName(“其他名称”);
}
context.batchStore(students.execute();
重新添加其他表时,应该能够使用与添加学生表相同的方法来执行此操作。如果您的数据库沿着这些线查看,其中一个学生有一个地址,但可能有许多爱好:

创建表地址(
id串行主键,
街道文本不为空
--其他领域等
); 
创建表学生(
id串行主键,
名称文本不为空,
等级int不为空,
地址\u id非空引用地址(id)
-- ...
);
创建表格爱好(
id串行主键,
学生id非空引用学生(id),
名称文本
-- ...
); 
假设您在同一个模式中拥有它,并且jOOQ已经在拾取您的学生表,您的
爱好
地址
表也将被拾取。然后,您可以像在常规sql中一样获取:

最终结果ret=context
.选择(Student.Student.ID,
Student.Student.NAME,
地址,地址,街道,
嗜好。嗜好。姓名)
.来自(学生,学生)
加入(爱好,爱好)
.on(Student.Student.ID.eq(Hobby.Hobby.Student\u ID))
.join(Address.Address)
.on(学生.学生.地址(ADDRESS.ADDRESS.ID))
.其中(学生.学生.成绩.情商(10))
.orderBy(Student.Student.NAME.asc())
.fetch();
整数lastStudentId=null;
for(最终记录val:ret){
最终整数studentId=val.getValue(Student.Student.ID,);
如果(!studentId.equals(lastStudentId)){
System.out.println(“学生”+val.getValue(Student.Student.NAME)
+“谁住在”+val.getValue(Address.Address.STREET)
+“街头有爱好:”
);
}
lastStudentId=studentId;
System.out.println(“\t”+val.getValue(Hobby.Hobby.NAME));
}

如果您的问题更多的是关于数据库的设计(即,您的地址是个人的,有自己的pk,还是属于学生所有,并且与学生的id相同,诸如此类),那么我认为这不是一个jOOQ问题,可能更适合在这里发布:。

谢谢你的及时回复@Brent Douglas。我已经试着实现了这一点,效果很好。但我的问题是,有没有一种方法可以了解JOOQ中给定表中外键引用的表类型。类似于我们在Hibernate中使用的相关字段映射到参考表,并使用Hibernate.initialise()加载?@msood如果这样做,我会感到惊讶。我认为jOOQ的要点是,它更像是具有便利性的直接sql