JOOQ使用转换器内联将字符串转换为枚举 [英] JOOQ Cast String to Enum with Converter inline

查看:152
本文介绍了JOOQ使用转换器内联将字符串转换为枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如此处所问,对此>#58538732 的后续问题

As asked here the follow up Question to this #58538732

根据Lukas Eder的建议,我编写了 EnumConverter 来转换 Integer DayOfWeek

As suggested by Lukas Eder I wrote an EnumConverter to convert the Integer to a DayOfWeek

public class DOWConverter extends EnumConverter<Integer, DayOfWeek>  {

    public DOWConverter()
    {
        super(Integer.class, DayOfWeek.class);           
    }               
}

select 现在看起来如下

DataType<DayOfWeek> typeDOW = SQLDataType.INTEGER.asConvertedDataType(new DOWConverter() /*ERROR*/);
Field<DayOfWeek> fieldDOW = DSL.field("{0}", typeDOW, lecture.DAY_OF_WEEK);

create.select( ..., fieldDOW, ...)...

出现错误消息:


线程 main中的异常java.lang.Error:未解决的编译问题:

不能访问类型为QueryFeaturesTask的封闭实例。必须使用封闭的QueryFeaturesTask类型的实例(例如xxNew A(),其中x是QueryFeaturesTask的实例)对分配进行限定。

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type QueryFeaturesTask is accessible. Must qualify the allocation with an enclosing instance of type QueryFeaturesTask (e.g. x.new A() where x is an instance of QueryFeaturesTask).

As声明,当前无法在CodeGen时使用Converter。

As stated, using the Converter at CodeGen Time is currently not an option.

推荐答案

似乎您放置了 DOWConverter 在另一个类中,从而创建一个内部舱。我建议您将转换器放在其自己的文件的顶层,使其成为顶层类。如果必须创建一个嵌套类,请通过使其成为静态来确保它不是内部类:

It seems that you placed your DOWConverter inside of another class, thus creating an inner class. I recommend you place the converter at the top level, in its own file, making it a top level class. If you must create a nested class, make sure it is not an inner class by making it static:

public class Enclosing {
    // Make this class here static:
    public static class DOWConverter extends EnumConverter<Integer, DayOfWeek> {
        public DOWConverter() {
            super(Integer.class, DayOfWeek.class);           
        }               
    }
}

Oracle的嵌套类教程对此做了很好的解释

这篇关于JOOQ使用转换器内联将字符串转换为枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆