Custom Enum Converter Issue

176 views
Skip to first unread message

Debapriya Patra

unread,
Mar 20, 2021, 12:21:00 PM3/20/21
to jOOQ User Group
Hi Lukas,

I have gone through the documentation(https://www.jooq.org/doc/latest/manual/sql-execution/fetching/data-type-conversion/) of how to use custom enum converter in JOOQ but I somehow could not make it work.

I have a table which has multiple columns and and one of the column is of type Integer. And while pulling the data from that table I wanted that Integer column value should convert to a ENUM value. Can you please help me understand how I can achieve that ?

Table: edu_document_type
-------------------------------------------
id Int (PrimaryKey)
document_type VARCHAR
document_display_name VARCHAR

Table : edu_content
--------------------------------
title
description
edu_document_type_id (ForeignKey from edu_document_type(id))


Java Entity: EduContent
--------------------------------------
String title;
String description;
String eduDocumentType;

Java Enum: EduDocumentType
--------------------------------------------------
public enum EduDocumentType {
SYLLABUS(1, "SYLLABUS"),
QUIZ(2, "QUIZ"),
EXAM_STUDY_GUIDE(3, "EXAM_STUDY_GUIDE"),
LECTURE_NOTES(4, "LECTURE_NOTES"),
EXAMS(5, "EXAMS"),
OTHER(6, "OTHER");

private Integer id;
private String documentType;

EduDocumentType(Integer id, String documentType) {
this.id = id;
this.documentType = documentType;
}

public String getDocumentType() {
return this.documentType;
}

public Integer getId() {
return this.id;
}

public static EduDocumentType findByDocumentType(final String documentType) {
return Arrays.stream(values())
.filter(value -> value.getDocumentType().equals(documentType))
.findFirst()
.orElse(null);
}

public static EduDocumentType findById(final Integer id) {
return Arrays.stream(values())
.filter(value -> value.getDocumentType().equals(id))
.findFirst()
.orElse(null);
}
}

Enum Converter
---------------------------
public class EduDocumentTypeConverter extends EnumConverter<Integer, EduDocumentType> {
public EduDocumentTypeConverter(Class<Integer> fromType, Class<EduDocumentType> toType) {
super(fromType, toType);
}
}

I am not clear when I am writing the JOOQ query to fetch data from edu_content table how I can use the converter to convert from (id)Integer to String(documentType). 

Is my converter correct ? Or shall I use the normal converter (Converter<U, T>)?

Please advise and need your help here.

Thanks,
Deba


Lukas Eder

unread,
Mar 22, 2021, 8:23:07 AM3/22/21
to jOOQ User Group
Hi Deba,

How did you configure your code generator?

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/7d7da370-5d1b-496d-9c5d-dece088ce532n%40googlegroups.com.

Debapriya Patra

unread,
Mar 22, 2021, 4:33:40 PM3/22/21
to jooq...@googlegroups.com
Hi Lukas,

Here is the groovy file used for the configuration.

jooq {
    db(sourceSets.main) {
        jdbc {
            driver = project.ext.jooqConfig.has('driver') ? project.ext.jooqConfig.driver : 'com.mysql.cj.jdbc.Driver'
            url = jooqConfig.url
            user = jooqConfig.user
            password = jooqConfig.password
            schema = jooqConfig.schema
        }
        generator {
            name = 'org.jooq.codegen.DefaultGenerator'
            strategy {
                name = 'org.jooq.codegen.DefaultGeneratorStrategy'
            }
            database {
               name = 'org.jooq.meta.mysql.MySQLDatabase'
               inputSchema = jooqConfig.schema
               excludes = 'API_OUTPUT_.*|DATABASECHANGELOG.*'
               forcedTypes {
                forcedType {
                  name =  project.ext.jooqConfig.has('disableBoolean') ? 'TINYINT' : 'BOOLEAN'
                  types = 'TINYINT'
                }
              }
            }
            generate {
               relations = true
               deprecated = false
               records = true
               pojos = true
               immutablePojos = false
               // fluentSetters = true
               daos = true
               validationAnnotations = true
               javaTimeTypes =  project.ext.jooqConfig.has('useJavaTimeTypes') ? true : false
            }
            target {
               packageName = project.ext.jooqConfig.has('generatedPackageName') ? project.ext.jooqConfig.generatedPackageName : (project.ext.has("mainPackageName") ? "com.chegg.${mainPackageName}.db" : 'com.chegg.' + project.name.replaceAll("-","_") + '.db')
               directory = 'build/src/generated/java'
            }
        }
    }
}

sourceSets {
    main {
        java {
            srcDir 'build/src/generated/java'
        }
    }
}

dependencies {
    jooqRuntime('mysql:mysql-connector-java:8.+')
}

Cheers,
Debapriya Patra
650.933.6852


Reply all
Reply to author
Forward
0 new messages