【问题标题】:jooq - Add code to the generated Record classjooq - 将代码添加到生成的 Record 类
【发布时间】:2014-03-19 09:28:48
【问题描述】:

我正在学习如何使用 jooq。我想知道是否可以在生成的 Record 类中添加一些域级别的方法。

假设记录是这样的:

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
   }

}

但是我知道如果我这样做,一旦我从数据库中再次生成这个类,我的所有更改都会丢失。那么推荐的方法是什么?

一个包装类?记录的子类?如果是其中任何一个,我如何让 jooq 在获取时识别这些类。例如:

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));
                   }
            });

在上述情况下,我为记录类提供了一个名为 ConCalInstance 的包装器。如果我需要使用包装器,是否必须为我执行的每个查询编写这样的 RecordHandler? 这样做的推荐方法是什么?

【问题讨论】:

    标签: java jooq


    【解决方案1】:

    您可以使用自己的扩展覆盖 jOOQ 的默认代码生成器。这在手册中有记录:

    这个例子展示了它是如何工作的:

    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())) {
                // [...]
            }
        }
    }
    

    【讨论】:

    • 嘿卢卡斯.. 感谢您的回复。对不起,我应该更清楚。这不是我要添加到模板中的内容。这些是领域方法。让我举个例子,CreditCardRecord 可以有一个类似 public boolean isAValidCard() 的方法。在这种方法中,我们将检查卡是否有效。 UserRecord 可以有不同的方法。这些不是您添加到模板中的内容,而是在需要时将其编码到对象中
    • 我可以使用 fetchInto POJO 方法,而不是使用生成的 Record.. 这是一种方法,我想。如何处理这种情况,您想为返回的记录添加更多功能
    • 我的解决方案仍然适用于您。让我再补充一句来澄清一下……事实上,由于您提到的原因,您不能手动将代码添加到生成的记录中。
    • 我开始添加代码,就像我更新的问题中提到的那样。请看看。有兴趣的人
    • @sethu:您实际上可以添加包含该更新的自己的答案,而不是修改问题。对于这个问题的未来访问者,了解您的解决方案是如何形成的会更加明显......
    【解决方案2】:

    根据 Lukas 的建议,我决定将代码添加到生成的记录中,如下所示

    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;
        }
    }
    

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

    这让我可以访问像这样的其他域行为...

    record.getBehaviour().doSomething();
    

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 2014-09-09
      • 2017-12-03
      • 2017-08-13
      • 2020-01-23
      • 1970-01-01
      • 2014-03-06
      • 2018-02-18
      • 2012-01-05
      相关资源
      最近更新 更多