Why can not user PageHelper.doPageAndSort to sort field which not in DO/Entity
Closed this issue · 4 comments
see these codes in io.choerodon.mybatis.pagehelper.OrderByParser#getColumn(Class<?>, String)
//前端传入字段与do对象字段对比,不匹配抛非法参数异常
for (EntityColumn entityColumn : columnList) {
if (entityColumn.getProperty().toLowerCase().equals(camelHumpProperty.toLowerCase())) {
return entityColumn.getColumn();
}
}
throw new IllegalArgumentException("Illegal sort argument: " + property);
now my project neet to sort the result of a sql using INNER JOIN, and the sort field is NOT in the basic table. therefore, I create a @Transient
field in entity/DO like this:
@Id
@GeneratedValue
private Long labelId;
@NotEmpty
@Size(max = 64)
private String labelName;
@NotEmpty
@Size(max = 32)
private String labelTypeCode;
@Transient
private String creator;
then I user this transient field to sort:
@GetMapping
public ResponseEntity<Page<Label>> list(
Label label,
@SortDefaults(
{@SortDefault(sort = Label.FIELD_LABEL_ID, direction = Direction.DESC),
@SortDefault(sort = Label.FIELD_CREATOR, direction = Direction.ASC)}
) PageRequest pageRequest
){
Page<Label> labels = this.labelService.pageAndRequest(pageRequest, label);
return ResponseEntity.ok(labels);
}
and when I request this api and get
IllegalArgumentException("Illegal sort argument: creator");
so WHY do we want to limit that the sort field must include in entity/DO? This is too strict to use.
THX
这个校验是对于单张表的查询校验,使用inner join的话属于多表联查,需要用map映射前端传入的排序字段和xml里sql中对应的字段(包括别名)。
choerodon-starter-mybatis-mapper简介
okay, this way is effective,
but it is not elegant enough :(
and look at #7
This problem has not been solved, why we close this issus
and how about set the table alias and column alias in @SortDefault?
such as
@SortDefault(sort = Label.FIELD_CREATOR, direction = Direction.ASC, trueColumnName="user_name", tableAlias=”l“)
that might be a good idea and i will make it as a reference, and #7 is closed by author.
thx