Order of attributes in MyBatis generated pojos

424 Views Asked by At

In our team we started using MyBatis Generator version 1.3.1, and we just recently migrated to version 1.3.2 and found a change in the order of attributes of the generated POJOs.

Previously, the order of the attributes in the generated classes was alphabetical, but after the change, we've realized that in the XxxKey classes, i.e., the classes that match the primary key of the table, the order of the attributes is no longer alphabetical.

Example:

Version 1.3.1:

public class PoolChargingKey {
    private String billingCycle;
    private Integer commercialGroupId;
    private Short destinationId;
    private Integer tariffPlanId;
    private String trafficCase;
    private Integer zoneId;
[...]

Version 1.3.2:

public class PoolChargingKey {
    private Integer commercialGroupId;
    private Integer tariffPlanId;
    private Integer zoneId;
    private Short destinationId;
    private String basicService;
    private String trafficCase;
[...]

We are accessing the generated POJOs with reflection for some mocking utils, and the change in the attributes order broke it. We can fix the affected changes, but it would be great to indicate how the classes are generated.

Is it possible to do that? I guess the answer is no, but just in case. By the way, the attribute order in version 1.3.1 was alphabetic. In which order are the attributes generated in version 1.3.2?

Regards, Tomas.

2

There are 2 best solutions below

0
On

I also asked this question in mybatis.user google groups, and Jeff Butler kindly answered this:

We did make a change here. This related to the following issue:

https://code.google.com/p/mybatis/issues/detail?id=438

Now the fields are kept in "key order". This means that they are in the order specified by the "KEY_SEQ" field returned from JDBC's DatabaseMetaData.getPrimaryKeys() function.

Jeff Butler

0
On

I haven't seen anything in the documentation regarding the configuration of the order of the attributes.

However, one way can be changing the source code as you desire. From org.mybatis.generator.codegen.mybatis3.model.IntrospectedTable.java, I saw that the order of columns added to the list are;

    List<IntrospectedColumn> answer = new ArrayList<IntrospectedColumn>();
    answer.addAll(primaryKeyColumns);
    answer.addAll(baseColumns);
    answer.addAll(blobColumns);

in getAllColumns() method. These added columns are List<IntrospectedColumn>. I thought maybe if you sort these lists according to the String actualColumnName in IntrospectedColumn class, maybe you can get an order you want.