Skip to content

DtoQuery may match the wrong constructor #3062

@spinachomes

Description

@spinachomes

when dto have some diffrent constructors,only match constructor with constructor's args length can't find the right constructor

  public DtoQueryPlan match(DtoMappingRequest request) {
    DtoColumn[] cols = request.getColumnMeta();
    int colLen = cols.length;
    // get the wrong constructor
    DtoMetaConstructor constructor = constructorMap.get(colLen);
    if (constructor != null) {
      return new DtoQueryPlanConstructor(request, constructor);
    }
    if (maxArgConstructor != null && colLen > maxArgConstructor.getArgCount()) {
      // maxArgConst + setters
      return matchMaxArgPlusSetters(request);
    }
    if (defaultConstructor != null) {
      return matchSetters(request);
    }
    String msg = "Unable to map the resultSet columns " + Arrays.toString(cols)
      + " to the bean type ["+dtoType+"] as the number of columns in the resultSet is less than the constructor"
      + " (and that there is no default constructor) ?";
    throw new IllegalStateException(msg);
  }

demo dto

@Data
@NoArgsConstructor
public class NameStatisticsResult {

    /**
     * 名称
     */
    private String name;

    /**
     * 部门名称,过渡字段
     */
    @JsonIgnore
    private String deptName;

    /**
     * 数量
     */
    private Integer value;

    /**
     * 详情
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<NameStatisticsResult> details;

    public NameStatisticsResult(String name, Integer value) {
        this.name = name;
        this.value = value;
    }

    public NameStatisticsResult(String name, Integer value, List<NameStatisticsResult> details) {
        this.name = name;
        this.value = value;
        this.details = details;
    }

    public NameStatisticsResult(String name, String deptName, Integer value) {
        this.name = name;
        this.deptName = deptName;
        this.value = value;
    }

}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions