1

I use jooq codegen to generate entity classes like this :

public class TCoactivitiesPinan extends BaseEntity implements Serializable {
private static final long serialVersionUID = 2007524284;
private Integer   id;
private String    openid;
private String    tel;
private Timestamp createdtime;
...}

but,I want it to automatically determine that if it's a time type, it automatically adds two fields. like this

public class TCoactivitiesPinan extends BaseEntity implements Serializable {
private static final long serialVersionUID = 2007524284;
private Integer   id;
private String    openid;
private String    tel;
private Timestamp createdtime;
private String createdtime_start; //  创建时间_开始时间
private String createdtime_end; //  创建时间_结束时间
private Integer   checkstate;
...
}

Is there any way to solve it? ths.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
hjk9317
  • 11
  • 1

1 Answers1

0

You can implement your own code generator and extend the POJO's "custom code section" as documented here:

https://www.jooq.org/doc/latest/manual/code-generation/codegen-custom-code

Essentially, just write a class like this:

public class MyGenerator extends JavaGenerator {

    @Override
    protected void generatePojoClassFooter(TableDefinition table, JavaWriter out) {
        for (ColumnDefinition column : table.getColumns()) {
            if (column.getType().getType().equals("TIMESTAMP")) {
                out.tab(1).println("private String %s_start; //  创建时间_开始时间", 
                    getStrategy().getJavaMemberName(column, Mode.POJO));
                out.tab(1).println("private String %s_end; //  创建时间_结束时间", 
                    getStrategy().getJavaMemberName(column, Mode.POJO));
            }
        }
    }
}

And then, configure it as follows:

<configuration>
    ...
    <generator>
        <name>com.example.MyGenerator</name>
        ...
    </generator>
</configuration>

Of course, the above solution will only generate the desired members, it will not generate any logic to populate those members when you use the generated POJO type.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • When I built it your way, the build failed. The error is as follows :[ERROR] Failed to execute goal org.jooq:jooq-codegen-maven:3.10.3:generate (default-cli) on project parkingmanage: Error running jOOQ code generation tool: com.exmp.MyGenerator -> [Help 1] -------------------- and pom like this:com.exmp.MyGenerator......... – hjk9317 Mar 02 '18 at 01:22
  • @hjk9317: I'm sure it works this way, but perhaps you have a classpath problem? Would you mind creating a new question with more details? – Lukas Eder Mar 04 '18 at 08:23