内部 class 作为休眠组件

Inner class as hibernate component

基本上,这可能吗

文档说

A component is a contained object that is persisted as a value type and not an entity reference.

Like value types, components do not support shared references. In other words, two persons could have the same name, but the two person objects would contain two independent name objects that were only "the same" by value.

The main distinguishing characteristic of a value type is the fact that they do not define their own lifecycle. We say that they are "owned" by something else (specifically an entity, as we will see later) which defines their lifecycle.

Non-static inner class 可以完美地适应这一点,但我从未见过实践中的例子。使用内部 classes 作为组件有什么限制吗?

JPA 规范说:

Embeddable classes must adhere to the requirements specified in Section 2.1 for entities with the excep- tion that embeddable classes are not annotated as Entity.

第 2.1 节说:

The entity class must be a top-level class.

经过一些研究我发现答​​案是不,这是不可能的

原因很简单,(在我看来)只与休眠工具本身有关,与任何体系结构问题无关。 Hibernate 要求组件 class 具有默认的无参数构造函数并且无法实例化内部 class(尽管它成功接受映射)。我希望 Hibernate 的未来版本将支持此功能。

您必须将内部组件 class 标记为 static