Here is the example code:
class Resource {
Long id;
@Embedded
GPU gpu;
CPU cpu;
}
abstract class GPU {
String name;
GpuType type;
}
class PhysicalGPU extends GPU {
}
class VirtualGPU extends GPU {
private int partition;
}
I have an Entity Resource
and it has a field GPU
which is a superclass. It can be a VirtualGPU
or PhysicalGPU
. But JPA or Hibernate seems not support class inheritance for @Embedded
fields. And it is ugly to create a single GPU type with no inheritance or create PhysicalGPUResource
and VirtualGPUResource
to implement the inheritance for a single field.
And I think as a descriptor of a Resource
, make the GPU
be an association in a seperate table (and give a ManyToOne
relation in Resource
) is also not a good practice but this will make the Inheritance available.
I did some search and I think right now the answer is no. But I still think this is a quite big requirement for the Hibernate ORM and it can be implemented in some framework like MyBatis
, it is quite weird that Hibernate do not support this behavior. So maybe there is some other way to make this done?
CodePudding user response:
I think current the best way is using UserType. Follow the article Custom Types in Hibernate and the @Type Annotation can be very helpful.
But I still don't know what is the advantage of the CompositeUserType
.