Hello Dennis,
2013/4/9 Dennis Fischer <[email protected]>:
> Hello,
>
> I'm changing parts of my code to remove magic numbers. I used Java enums for
> that - which make the code nice and clean.
> Some enum classes use more advanced features - like conversions methods, a
> custom toString method etc.
> Now it comes to the time to map these to the database in especially to jooq.
> I've wanted to use codegen for all steps - but failed - I got following enum
> class:
>>
>> public enum Visibility {
>> PUBLIC("visibilitylist.public"), UNLISTED("visibilitylist.unlisted"),
>> PRIVATE("visibilitylist.private"), SCHEDULED(
>> "visibilitylist.scheduled");
>>
>> private String i18n;
>>
>> private Visibility(final String i18n) {
>> this.i18n = i18n;
>> }
>>
>> @Override
>> public String toString() {
>> return TextUtil.getString(i18n);
>> }
>> }
>
> The data should be stored as string in the database. I've already wanted to
> use / write my own Converter implementation for this, but the documentation
> references "EnumConverter" class but I didn't get it working:
> I've tried:
>
>> public VisibilityConverter extends EnumConverter<String, Visibility> { }
>
>
> However this forces me to create a constructor and then it's impossible to
> use this with default codegen.
Because of Java's generic type erasure, you have to provide the
EnumConverter base type with Class instances of String.class and
Visibility.class. The idea here is that you implement a default
constructor as such:
public VisibilityConverter extends EnumConverter<String, Visibility> {
public VisibilityConverter() {
super(String.class, Visibility.class);
}
}
You should then be able to use this converter in generated code.
> I then tried to register just the EnumConverter - which failed again.
> I don't want to create any new table for the enums - they should just be
> mapped to.
>
> Could you show me a short example how to use EnumConverter properly?
>
> For the codegen I used described registration:
>>
>> <customType>
>> <name>....Visibility</name>
>> <converter>....VisibilityConverter</converter>
>> </customType>
>
> ....
>>
>> <forcedType>
>> <name>....Visibility</name>
>> <expressions>.*\.VISIBILITY</expressions>
>> </forcedType>
This looks correct to me.
Let me know if this helps. In any case, I will review the manual and
see if some more clarification could be added:
https://github.com/jOOQ/jOOQ/issues/2394
Cheers
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.