Discriminator columns and subtypes

65 views
Skip to first unread message

Marcel Overdijk

unread,
Aug 29, 2018, 8:06:21 AM8/29/18
to SimpleFlatMapper
Hi,

Does SimpleFlatMapper support mapping to specific types using a discriminator column?

E.g. I have an abstract Employee class with 2 subclasses: InternalEmployee and ExternalEmployee.
In the database this is stored in a single table having a type column containing the value "I" or "E". It's a legacy db model that cannot be changed.

Wen querying a list of Employees is it possible to map to the correct subclass using SimpleFlatMapper?


Cheers,
Marcel

Arnaud Roger

unread,
Aug 29, 2018, 8:48:59 AM8/29/18
to mar...@overdijk.me, SimpleFlatMapper
There is a Discriminator mapper thingy, that as far as I can recall would work only at the root type - it is quite a pain to do with the interaction with the joins.

you can look at those unit tests for some examples


--
You received this message because you are subscribed to the Google Groups "SimpleFlatMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simpleflatmapp...@googlegroups.com.
To post to this group, send email to simplefl...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/simpleflatmapper/d6964763-290e-4eaf-bcad-b8cd2635bab7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcel Overdijk

unread,
Aug 29, 2018, 5:08:16 PM8/29/18
to SimpleFlatMapper
Thanks,

I got the basics working.
Based on the .<Employee>newDiscriminator("type") and the .when definitions the correct subclassed is used to map the record to.

The thing I don't get to work yet is that the InternalEmployee class has a salary property, but the database column is named "internal_salary".
All columns name in the employee that are for specific type are prefixed with type. Just like ExternalEmployee has an hourRate property and the employee table contains an external_hour_rate column.

Is this something that can be mapped?

In the link with the test case I see .addMapping("name", 3, FieldMapperColumnDefinition.<JdbcColumnKey>identity()) but I don't actually understand how I can use that..

Arnaud Roger

unread,
Aug 29, 2018, 5:15:41 PM8/29/18
to mar...@overdijk.me, SimpleFlatMapper
you can add alias if the column in the db don't match the exact property path

JdbcFactory.newInstance().addAlias("internal_salary", "salary")

would that solve the problem?

Marcel Overdijk

unread,
Aug 29, 2018, 5:20:36 PM8/29/18
to SimpleFlatMapper
Yes that worked!

I had to add the alias before the discriminator.

It's looking good so far, next step is to include those employees as part of a query retrieving departments. 

Marcel Overdijk

unread,
Aug 30, 2018, 2:59:06 AM8/30/18
to SimpleFlatMapper
Is it possible to use (nested) discriminator classes like:

JdbcMapper<Department> departmentJdbcMapper = JdbcMapperFactory
        .newInstance()
        .keys("id", "employee_id")
        .<Employee>newDiscriminator("type")
        .when("Internal", InternalEmployee.class)
        .when("External", ExternalEmployee.class)
        .mapper(); // returns a JdbcMapper<Employee> instead of a JdbcMapper<Department>

The problem is that the mapper() method returns the nested discriminator class instead of the top class?

Arnaud Roger

unread,
Aug 30, 2018, 3:11:00 AM8/30/18
to Marcel Overdijk, SimpleFlatMapper
It’s not possible at the minute, would need to have a look at how I can do that in theory it might be possible. Though I’m in the middle of rewriting all the csv mapping to fit in the regular model so would not happen just now

Alternatively a solution might to use a builder for your employee that would instantiate the right class. If you add a static builder method on the root type Sfm should pick it up

Sent from my iPhone

Marcel Overdijk

unread,
Sep 5, 2018, 9:03:54 AM9/5/18
to SimpleFlatMapper
Is it possible to have the builder outisde of the root type itself? (I cannot modify the root type class).
Reply all
Reply to author
Forward
0 new messages