I have an entity and specialized class which is derived from the entity.
Is there any way (something tricky in JPA or spring) to let JPA create instances of the derived class instead of the entity class?
Otherwise I would need to create ‘copy constructors’ for each derived class. Or replace the inheritance by delegates wich seems to be clunky.
If your entity has many subclasses that should be stored in the database differently, you can use JPA inheritance support (
@Inheritance, etc).If your entity has only one subclass that should be always used in place of that entity, you can annotate that subclass as
@Entity, and the entity – as@MappedSuperclass.If you want to achieve the previous case without modifying entities, you can use provider-specific solutions. For example, in Hibernate you can register a custom
Interceptorand override itsinstantiate()method.