java - 如何在 Hibernate 中映射父级的 bean 类变量?

标签 java hibernate

我有一个名为 Bean_A 的 Bean 类,它映射到 table_A,并且这个 Bean_A 类是从 Bean 基类继承的,并且 Bean 基类将有一个 unique_reference_key 变量,该变量将与 Bean_A 类一起保存到数据库中。我已经为 Bean_A 和 table_A 中的 unique_reference_key 字段准备好了 hibernate 文件。

我可以知道如何在 hibernate 中映射基 bean 类,以确保在保存 Bean_A 对象时将 unique_reference_key 变量保存到数据库中吗?请注意,此基 bean 类未映射到数据库中的任何表,并且不存在“代表”基 bean 的此类表。

在提出这个问题之前,我查阅了下面的文章,但仍然没有得到任何线索。

  1. Hibernate simplifies inheritance mapping
  2. Hibernate Chapter 5.Basic O/R Mapping

最佳答案

如果您添加一些代码,您的描述会更加清晰。但我认为您只是在寻找 the documentation 中描述的 MappedSuperclass 注释。 :

5.1.6.4. Inherit properties from superclasses

This is sometimes useful to share common properties through a technical or a business superclass without including it as a regular mapped entity (ie no specific table for this entity). For that purpose you can map them as @MappedSuperclass.

@MappedSuperclass
public class BaseEntity {
    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    public Date getLastUpdate() { ... }
    public String getLastUpdater() { ... }
    ...
}

@Entity class Order extends BaseEntity {
    @Id public Integer getId() { ... }
    ...
}

关于java - 如何在 Hibernate 中映射父级的 bean 类变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10534679/

相关文章:

带有 Hibernate 的 Spring Boot - 从另一个项目 Autowiring 存储库

java - 将具有外键的实体对象级联保存为复合主键的一部分

spring - 为什么 hibernate 实体图获取嵌套的惰性集合

java - Java中的MSCAPI证书选择框;太阳MSCAPI?

java - 未找到属性文件 - 如何将其定位为资源?

java - 如何获取多个可运行对象共有的变量的最小值?

java - 使用 ebean 或 hibernate 将值插入到一对一映射注释中

java - 使用 Vararg 方法进行 Hibernate Criteria 查询

java - 打印 Java 应用程序的命令行参数数量

java - DataAccessException 无法捕获