I learning how to work with jooq. I would like to know if I can add some domain-level methods in to the generated Record classes.

Suppose the record was this:

public class ConCalCompanyRecord extends org.jooq.impl.UpdatableRecordImpl<com.aesthete.csmart.connect.model.db.gen.tables.records.ConCalCompanyRecord> implements org.jooq.Record6<java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String, java.sql.Timestamp, java.sql.Timestamp> {

// properties
// getters and setters

// I would like to add a method like this:
   public void isABlueCompany(){
     // work with the fields
   }

}

But I know if I do this, as soon as I generate this class again from the DB, all my changes will get lost. So what is the recommended way of doing this?

A wrapper class? A sub class to the record? If its any of these, how do I get jooq to recognise these classes at the time of fetching. For example:

connectionFacade.getDSLContext()
            .selectFrom(CON_CAL_INSTANCE)
            .where(CON_CAL_INSTANCE.DATE.between(
                    new Date(datesOfTheWeekForDate[0].toDate().getTime()), new Date(datesOfTheWeekForDate[1].toDate().getTime())))
            .orderBy(CON_CAL_INSTANCE.DATE)
            .fetch()
            .into(new RecordHandler<ConCalInstanceRecord>() {
                @Override
                public void next(ConCalInstanceRecord record) {
                    calendarEntries.addToList(new com.aesthete.csmart.connect.model.domain.records.ConCalInstance(record));
                   }
            });

In the above case I am providing a wrapper called ConCalInstance to the record class. Do I have to write a RecordHandler like this for every query I execute if I need to use a wrapper? What is the recommended way of doing this?

有帮助吗?

解决方案 2

Based on Lukas's suggestion I landed up adding code to my generated Record like this

public class ConCalInstanceRecord extends org.jooq.impl.UpdatableRecordImpl....{

     //fields and getter and setters of the generated record..

    private ConCalInstanceBehaviour behaviour;

    public ConCalInstanceBehaviour getBehaviour(){
        if(behaviour==null){
            behaviour=new ConCalInstanceBehaviour(this);
        }
        return behaviour;
    }
}

Sort of like the wrapper like I was talking about, but the other way around, the record wraps a behaviour class. I can now add custom behaviour into my behaviour classes without having to go back to the generator every time I needed to add a new method.

This allowed me to access additional domain behaviour like this...

record.getBehaviour().doSomething();

其他提示

You can override jOOQ's default code generator with your own extensions. This is documented here, in the manual:

The example shows how it works:

public class MyGenerator extends JavaGenerator {

    @Override
    protected void generateRecordClassFooter(
        TableDefinition table, 
        JavaWriter out
    ) {
        super.generateRecordClassFooter(table, out);

        if ("SOME_TABLE".equals(table.getName())) {
            out.println();
            out.tab(1).println("public void isABlueCompany() {");
            out.tab(2).println("// Your logic here");
            out.tab(1).println("}");
        }
        else if ("SOME_OTHER_TABLE".equals(table.getName())) {
            // [...]
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top