ModelMapper与Jooq Record集成

时间:2013-08-30 13:31:41

标签: java jooq modelmapper

===== POJO =====

// Employee POJO
     @JsonInclude(JsonInclude.Include.NON_NULL)
     @JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
     public class Employee implements Serializable {
       private Integer id;
       private String name;
       private Integer companyId;
       // assume getters ,setters and serializable implementations.
     }
// Company POJO
     @JsonInclude(JsonInclude.Include.NON_NULL)
     @JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
     public class Company implements Serializable {
       private Integer id;
       private String name;
       // assume getters ,setters and serializable implementations.
     }

// EmployeeVO POJO
     @JsonInclude(JsonInclude.Include.NON_NULL)
     @JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
     public class EmployeeVO implements Serializable {
       private Employee employee;
       private Company company;

       // assume getters ,setters and serializable implementations.
     }

===== 我的DAO图层类 =====

public List<EmployeeVO> getEmployees(){     
    // configuring model mapper.
       ModelMapper modelMapper = new ModelMapper();
       modelMapper.getConfiguration()
                  .addValueReader(new RecordValueReader())
              .setSourceNameTokenizer(NameTokenizers.UNDERSCORE);

    //property map configuration.
      PropertyMap<Record, EmployeeVO> employeeVOMap = new PropertyMap<Record, EmployeeVO>() {
       protected void configure() {
          map().getEmployee().setName(this.<String>source("name"));
          map().getEmployee()..setId(this.<Integer>source("id"));
          map().getCompany().setName(this.<String>source("comp_name"));
          map().getCompany().setId(this.<String>source("comp_id"));
        }
     };
    // TypeMap config
    modelMapper.createTypeMap(Record.class, EmployeeVO.class);
    // adding employeeVOMap .
    modelMapper.addMappings(employeeVOMap);         

// JOOQ query  
    List<Field<?>> fields = Lists.newArrayList();  
    //  fields includes, id, name, comp_name, comp_id

    SelectJoinStep query = select(dslContext, fields).from(EMPLOYEE)
                                       .join(COMPANY)
                                       .on(COMPANY.ID.equal(EMPLOYEE.COMPANY_ID));    
        Result<Record> records = query.fetch();

    Record record = null;
        Iterator<Record> it = records.iterator();
        List<EmployeeVO> employeeList=  Lists.newArrayList();
        while (it.hasNext()) {
            record = it.next();

            EmployeeVO employeeVOObj =
                    modelMapper.map(record, EmployeeVO.class);
            employeeList.add(employeeVOObj);
        }
    return employeeList;    
}

===== 错误日志 =====

1)错误mapping org.jooq.impl.RecordImpl to com.myportal.bingo.db.model.EmployeeVO     1错误]有根本原因     java.lang.ArrayIndexOutOfBoundsException:-1

注意: 当ModelMapper到达下面的方法时抛出上述异常。

private void matchSource(TypeInfo<?> sourceTypeInfo, Mutator destinationMutator)ImplicitMappingBuilder.java

sourceTypeInfo.getAccessors() is null.

任何帮助?

1 个答案:

答案 0 :(得分:0)

有同样的问题,或者至少看起来一样。 (您可以在最后一段中直接转到我的解决方案。)大量调试显示以下内容:

如果该行上的访问者(在您的问题中提到)为null,则执行TypeInfoImpl类中的accessors = PropertyInfoSetResolver.resolveAccessors(source, type, configuration)行,并且在我的情况下出现异常的原因是此调用:

valueReader.get(source, memberName)在PropertyInfoSetResolver类的'resolveAccessors'方法的以下代码段中:

if (valueReader == null)
  resolveProperties(type, true, configuration, accessors);
else {
  NameTransformer nameTransformer = configuration.getSourceNameTransformer();
  for (String memberName : valueReader.memberNames(source))
    accessors.put(nameTransformer.transform(memberName, NameableType.GENERIC),
        new ValueReaderPropertyInfo(valueReader, valueReader.get(source, memberName),
            memberName));

最终在source.getValue(memberName.toUpperCase()),其中source是JOOQ的记录;在我的案例中InvoiceRecord。并且 - tada - 由于某种原因invoice.getValue("INVOICE_ID")最终出现在异常中(没有这样的字段,因此indexOf返回-1导致ArrayIndexOutOfBoundsException),而invoice.getValue("invoice_id")完全正常。

所以else条件(上面相同的代码段)不是执行代码的正确方法,并且if案例证明是正常的。

这就是在我的特定情况下帮助我的原因:删除行modelMapper.getConfiguration().addValueReader(new RecordValueReader())。希望这对你也有帮助。