JPA:是否有某种方法可以实现可嵌入的继承
以下是示例代码:
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;
}
我有一个实体Resource
,它有一个字段GPU
,它是一个超类。它可以是VirtualGPU
或PhysicalGPU
。但 JPA 或 Hibernate 似乎不支持 @Embedded
字段的类继承。创建没有继承的单一 GPU 类型或创建PhysicalGPUResource 和VirtualGPUResource 来实现单个字段的继承是很丑陋的。
我认为作为 Resource
的描述符,使 GPU
成为单独表中的关联(并在 ManyToOne
关系中给出>Resource)也不是一个好的实践,但这将使继承可用。
我做了一些搜索,我想现在的答案是否定的。但我仍然认为这对于 Hibernate ORM 来说是一个相当大的要求,它可以在像 MyBatis 这样的框架中实现,很奇怪的是 Hibernate 不支持这种行为。那么也许还有其他方法可以做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为当前最好的方法是使用USERTYPE。遵循文章 hibernate中的自定义类型和@Type注释可以很有帮助。
但是我仍然不知道
CompositeUsertype
的优势是什么。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
.