Data type conversion with generic types

221 views
Skip to first unread message

Kirusanth Poopalasingam

unread,
Apr 21, 2017, 6:08:30 AM4/21/17
to jOOQ User Group
Hello Lukas,

First, thank you for creating jOOQ ( and giving a serious alternative to JPA / Hibernate). We use jOOQ heavily in our projects.

For primary and foreign keys, we have a custom UUID class with generics like MyUuid<T> and added a Converter from java.util.UUID to our MyUuid.

With the generic-MyUuid, you get an additional type safety. Now the problem is, that the converter does not respect the generic type and generates Methods like:

public class MyTableRecord ... {
 
// ...
 
public void setId(MyUuid value) { // better: setId(MyUuid<MyTableRecord> value)
 setValue
(0, value);
 
}


 
public void setOtherFk(MyUuid value) { // better: setOtherFk(MyUuid<OtherRecord> value)
 setValue
(6, value);
 
}
 
// ...
}



Is there a way to hook into the code-generation process to set the generic types-correctly.

( Tested with jOOQ 3.9.1 and PG 9.1)

Thank you,
Kiru

Lukas Eder

unread,
Apr 21, 2017, 6:19:15 AM4/21/17
to jooq...@googlegroups.com
Hi Kiru,

Thanks for your nice words.

It's perfectly possible to register converters that use generics, but perhaps you haven't configured the code generator correctly? Can you please post your converter and your code generator configuration?

Thanks,
Lukas

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kirusanth Poopalasingam

unread,
Apr 21, 2017, 8:11:36 AM4/21/17
to jOOQ User Group
Hi Lukas,

This is the configuration and converter:

// skipped jdbc 

Converter<UUID, MyUuid> uuidToMyUuid = Converter.of(
   
UUID.class,
   
MyUuid.class,
   
MyUuid::create,
    myUuid
-> UUID.fromString(myUuid.asString())
);

Configuration configuration =
   
new Configuration()
       
.withJdbc(jdbc)
       
.withGenerator(new Generator()
           
.withStrategy(new Strategy()
               
.withName(DefaultGeneratorStrategy.class.getName()))
           
.withDatabase(new Database()
               
.withIncludes(".*")
               
.withExcludes("")
               
.withInputSchema("public")
               
.withCustomTypes(
                   
new CustomType()
                       
.withName(uuidToMyUuid.toType().getName())
                       
.withType(uuidToMyUuid.toType().getName())
                       
.withConverter(uuidToMyUuid.getClass().getName())
               
)
               
.withForcedTypes(
                   
new ForcedType()
                       
.withName(uuidToMyUuid.toType().getName())
                       
.withTypes("UUID")
               
)
           
)
           
.withGenerate(new Generate()
               
.withDaos(false)
               
.withPojos(false)
               
.withRecords(true)
               
.withInterfaces(false)
           
)
           
.withTarget(new Target()
               
.withPackageName("")
               
.withDirectory("../src/main/java"))
       
);

GenerationTool.generate(configuration);



Thanks,
Kiru
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.

Lukas Eder

unread,
Apr 21, 2017, 8:36:33 AM4/21/17
to jooq...@googlegroups.com
Hi Kiru,

I'm sorry for the round trip. I must have missed some detail in your original mail. What you really want is a generic record reference of the primary key record type in both the primary key MyUuid and in all referencing foreign key MyUuid.

There had actually been similar ideas in the past, I believe on this mailing list, and perhaps even as GitHub issues (although I cannot seem to find them). Such a type system enhancement would allow for enforcing JOIN predicate comparisons to compile only for matching primary key / foreign key or foreign key / foreign key pairs.

A similar issue is this one:

But I'll create a new one to be sure we won't forget:

Certainly a very good idea. I don't think there's an easy workaround right now to achieve the same.

To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.

Poopalasingam Kirusanth

unread,
Apr 21, 2017, 10:29:06 AM4/21/17
to jooq...@googlegroups.com
Hi Lukas,

Thank you for creating the issue, I'll track it further.

I tried to overwrite the JavaGenerator.generateRecordGetter() but you there you don't have access to the TableDefiniton.

Did the following hack to get it working, but this only works for the primary key. For the FK, I don't know how to get the target record class name:


Thanks,
Kiru





--
You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/53RZqoewa3g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Mit freundlichen Grüssen
Kirusanth Poopalasingam

Lukas Eder

unread,
Apr 21, 2017, 10:35:44 AM4/21/17
to jooq...@googlegroups.com
I don't think you can override anything in the code generator. This must be implemented in jOOQ-meta (i.e. as a data type). If you'd like to continue trying, you could extend your jOOQ-meta Database in order to manage the DataTypeDefinition of your primary key / foreign key columns. This might work at least for non-composite keys.

Let me know if you need some additional pointers.
Lukas

Kirusanth Poopalasingam

unread,
Apr 28, 2017, 5:14:36 AM4/28/17
to jOOQ User Group
Hello Lukas,

I am not really familiar with the jooq codebase. I tried to extend PostgresDatabase, and overwrite loadPrimaryKeys, but I miss to find where exactly the correct DataTypeDefinition is assigned to each ColumnDefinition.

Could you point me to the correct direction? 

Thanks,
Kiru

--
You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/53RZqoewa3g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Mit freundlichen Grüssen
Kirusanth Poopalasingam

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.

Lukas Eder

unread,
Apr 28, 2017, 7:32:28 AM4/28/17
to jooq...@googlegroups.com
Hi Kiru,

Have a look at the PostgresDatabase.getTables0() method, which returns PostgresTableDefinition instances, which in turn create DefaultDataTypeDefinition instances, e.g. here:

Of course, using an IDE, you can track down all references to DataTypeDefinition and all calls to the DefaultDataTypeDefinition constructor to find possible other places that you might need to touch.

Hope this helps,
Lukas

To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages