Dynamic Inheritance
I have a situation where I want a class (which is a JPA entity) to be able to extend either class A or class B dynamically. My thought was to use Generics, but it looks like generics doesn’t support this. For example:
@Entity
public abstract class Resource() {
...
}
@Entity
public abstract class Snapshot() {
...
}
public abstract class CommonRS<R extends Resource, S extends Snapshot> {
/* This is the class that I want to dynamically assign Inheritance to. */
...
}
@Entity
public class FeedResource extends CommonRS<Resource> {
...
}
@Entity
public class FeedSnapshot extends CommonRS<Snapshot> {
...
}
The reason I want to do this is that FeedResource must Inherit from Resource and FeedSnapshot must inherit from Snapshot because both classes are using the JPA join strategy InheritanceType.JOINED
and are persisted to different tables, however they both share common attributes and I would like them to be able to inherit those common attributes.
I understand that I can I could use @Embeddable on CommonRS and the Embed it in both FeedResource and FeedSnapshot.
Since Java doesn’t support multiple inheritance, I can’t see any other way to do this other than using Embeddable.
Thanks in advance.