Best practices to store Enum class name and enum value in database

22 views
Skip to first unread message

Raghavan Lakshmana

unread,
May 15, 2019, 11:35:55 AM5/15/19
to jOOQ User Group

I would like to seek help from this community on a problem I am trying to solve in my application which used jooq.


I have posted this question on stackoverflow. Here is the post,


I have a class with different enums, for example:


class EligibilityRule{
ProductEligibility productEligibility;
CountryEligibility countryEligibility
}

enum ProductEligibility{
PRODUCT_X,
PRODUCT_Y
}

enum CountryEligibility{
US,
IN,
CN
..
}


I want to store these enum class name and their value to database table called eligibility rule, and my table schema looks likes this,


String id  => auto_increment id
String ruleType => enum class name (ex: ProductEligibility)
String ruleValue => enum value (ex: PRODUCT_X)


I am using JOOQ, in the past I had used forced type to just store the enum value. But, in this case I want to store enum class name and enum value. I also want to reconstruct the enum object when I query the records from db.


Are there any patterns I can follow or is there any functionality in JOOQ which I can extend to solve this problem?

Lukas Eder

unread,
May 16, 2019, 3:24:29 AM5/16/19
to jooq...@googlegroups.com
Thanks for your message.

There isn't any reliable way to map an EAV (Entity Attribute Value) table in a type safe manner, because the whole point of an EAV table is to remove type safety by applying denormalisation. I personally advise against using EAV in most cases (with some exceptions), but you may have your reasons.

I hope this helps,
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+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/99e41715-2f3a-4983-9c32-6e929e598543%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages