How to create mapping in hibernate that depends on the type property, insert/retrieve the data into/from proper column.
Structure:
TABLE COLUMNS:
|TYPE | CHARACTER | DATE | TIME | NUMERIC|
POJO:
class Pojo {
private int type;
private Object data;
...
}
Examples:
-
Insert/Update
If the type is 1 we input the value to column CHARACTER -
Select
If the type is 2 we get the value from column NUMERIC
TIP:
The structure where we have two columns and we PIVOT the result is not a option for this case.
The solution that i have choose for this is table per class hierarchy mapping with an integer discriminator.
I have created a base abstract generic class that store the object
For Type management i have created and interface, but there is possible to enumeration using the encapsulation presented by Arthur.
The for each type I have created class
TIP: To allow use the a number as discriminator value we need to specify it first for the class that we are mapping, because by default is used class name [String] and i we try to use the numeric discriminator for subclass mapping we will get a hibernate error say that there is some type mismatch.
While we retrieve the object from database is always in given typed class that we can operate easily on it, and we use one place to save them by saving the base class.