2

===== 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.

有什么帮助吗?

4

1 回答 1

0

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

如果该行上的访问器(在您的问题中提到)为空,则accessors = PropertyInfoSetResolver.resolveAccessors(source, type, configuration)执行 TypeInfoImpl 类中的行,在我的情况下异常的原因是这个调用:

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));

以 结尾, JOOQ 的记录在source.getValue(memberName.toUpperCase())哪里;sourceInvoiceRecord 在我的情况下。而且 - tada - 由于某种原因invoice.getValue("INVOICE_ID")最终出现异常(没有这样的字段,因此indexOf返回-1导致 ArrayIndexOutOfBoundsException),而invoice.getValue("invoice_id")完全没问题。

所以else条件(上面的同一段代码)不是执行代码的正确方法,if结果证明是可以的。

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

于 2013-08-30T21:42:01.150 回答