I have an entity class which I need to use at multiple places. So I created a class with generics as below
class Output<T> {
...
private String referencedEntityName;
private T referencedEntity;
...
}
I've two other classes which are using this class
class Process {
...
private List<Output<Process>> outputs;
...
}
Another class which uses Output<T>
is
class Machine {
...
private List<Output<Machine>> outputs;
...
}
When stored in database it should look like this
+----+------------------------+----------------------+
| id | referenced_entity_name | referenced_entity_id |
+----+------------------------+----------------------+
| 1 | Process | 123 |
+----+------------------------+----------------------+
| 2 | Process | 234 |
+----+------------------------+----------------------+
| 3 | Machine | 345 |
+----+------------------------+----------------------+
How do I achieve this?
P. S. This is not exactly an inheritance problem like this. Though it could be solved using an interface solution like DiscriminatorValue
but I'm not sure.
Here, there is a relation between two entities, one entity is referred in a few other entities. I think that should be clear by the use of generics. Also, this solution could be designed in some other way by maintaining the same relations. I'm open to that as well.