Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Single table inheritance: Using foreign key as discrimination values
Single table inheritance: Using foreign key as discrimination values [message #1708364] Tue, 15 September 2015 22:19 Go to next message
Naga Gangadhar Reddy is currently offline Naga Gangadhar ReddyFriend
Messages: 1
Registered: September 2015
Junior Member
I have this scenario where employee has a column called employee_type_id which has details about the type.

employees
id name employee_type_id
1 Mark 1
2 John 3
3 Dough 2

employee_types
id role description
1 developer writes code
2 manager managed teams
3 QA tests code

Here to use Single Table inheritance for employee entity, I have to use employee_type_id which is foreign_key. But I want role column of employee_types table to be the discriminator value as integer does not make sense. How can I achieve this?
Please help.



Re: Single table inheritance: Using foreign key as discrimination values [message #1708670 is a reply to message #1708364] Fri, 18 September 2015 14:36 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1380
Registered: July 2009
Senior Member
I wouldn't use java inheritance for what you've just shown. An Employee can have the developer role, a manager role, or a QA role, so I would make the Employee reference a Type entity and just use it like that. This allows an Employee to change roles later on, something that you can't to do with Java inheritance: a QA subclass cannot be changed into a Manager subclass, and would have to be deleted and recreated as a Manager, while this way, any Employee with a developer type role can just change roles to a manager type role.

If there is only 1 developer type record, and if they really are static and tied to inheritance, then you would have two mappings to the same field. Use the foreign key as the discriminator, and the 1:1 mapping to the type with the join column marked as insertable=false, updatable=false so that it is read-only and can never be changed. It shouldn't matter if you use the developer string or the foreign key value if they really are static. I'm guessing this won't work for your model though.

If each employee has its own record in the employee_types table, then the Employee entity can use this table as a secondary table. This doesn't quite match what you have though, as this requires a strict 1:1, and generally uses the same ID values for both, with the ID in the secondary table controlled by the value in the primary table - this allows sequencing to be used, and prevents conflicts.

If your table is more complex, such that a developer Mark might have a different description from Developer Peter:
employee_types
id role description
1 developer 'writes code'
4 developer 'reviews code'

But still need 3 distinct Employee class types for reasons you haven't shown, then you need something more complex then regular JPA inheritance. EclipseLink gives you the option to define your own way to decide what an Entity's class type should be using the ClassExtractor: http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_classextractor.htm This still may need you to force a join between the two tables so that the 'role' string is available when the Employee instances are being built, so that your class extractor method has all the info it needs to build the correct type.

Hope this helps.
Chris

[Updated on: Fri, 18 September 2015 14:43]

Report message to a moderator

Previous Topic:Eclipselink + maven+ jpa
Next Topic:Eclipse Moxy API - Unmarshaller encoding issue
Goto Forum:
  


Current Time: Sun Feb 28 14:03:29 GMT 2021

Powered by FUDForum. Page generated in 0.23768 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top