jooq - Add code to the generated Record class

set*_*thu 5 java jooq

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
   }

}
Run Code Online (Sandbox Code Playgroud)

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));
                   }
            });
Run Code Online (Sandbox Code Playgroud)

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?

set*_*thu 3

根据卢卡斯的建议,我最终将代码添加到生成的记录中,如下所示

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

有点像我所说的包装器,但相反,记录包装了一个行为类。现在,我可以将自定义行为添加到我的行为类中,而无需每次需要添加新方法时都返回生成器。

这使我能够访问像这样的其他域行为......

record.getBehaviour().doSomething();
Run Code Online (Sandbox Code Playgroud)