What you think cannot be mapped to SQL as is. Suppose you have Entity1 with fields field1A , field1B ... and Entity2 with fields field2A , field2B , ... Now you want to run the following query:
SELECT Entity1.* FROM Entity1 UNION SELECT Entity2.* FROM Entity2 ORDER BY CommonField
which is impossible in the SQL world, because entities have a different number of fields and different types of fields.
So, you need to think about extracting the common fields into a separate CommonEntity table, converting your interface into a standalone one-to-one Entity1 with Entity1 and Entity2 (see the table for the subclass ). Then SQL will look like this:
SELECT * from CommonEntity LEFT OUTER JOIN Entity1 ON Entity1.refId = CommonEntity.id LEFT OUTER JOIN Entity2 ON Entity2.refId = CommonEntity.id ORDER BY CommonField
Or you can create a view from your tables and introduce an artificial discriminator (the discriminator is what will “distinguish” identifiers from different tables, which caused a problem in your solution ), and then match the entity with this view (so we get a table for the hierarchy classes ):
CREATE VIEW EntityAandEntityB AS SELECT 'A' as discriminator, Entity1.ID, CommonField1, ... CommonFieldZ, Entity1.field1A, ... Entity1.field1N, NULL, NULL, ... NULL(M) FROM Entity1 UNION SELECT 'B' as discriminator, Entity2.ID, CommonField1, ... CommonFieldZ, NULL, NULL, ... NULL(N), Entity2.field2A, ... Entity2.field2M FROM Entity2 ORDER BY CommonField1, ...
Other alternatives (for example, those mentioned by @UdoFholl , which are also some kind of “outer join” for EntityAandEntityB ) will result in 2 SQL and, therefore, order an “entire” query, and scrolling is not possible .