JOOQ Cast String to Enum with Converter inline

為{幸葍}努か 提交于 2019-12-13 17:19:20

问题


As asked here the follow up Question to this #58538732

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

The select now looks the following

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

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

With the Error Message:

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 stated, using the Converter at CodeGen Time is currently not an option.


回答1:


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's tutorial on nested classes explains this really well.



来源:https://stackoverflow.com/questions/58586868/jooq-cast-string-to-enum-with-converter-inline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!