首页 > 解决方案 > 我可以创建另一个只有 2 个字段的实体来映射到同一个表吗

问题描述

以下是只有 2 个字段的表格:

 customer_order_base { cust_id, cust_lookup_fact }

这已经映射到CustomerOrderBase使用这两个字段调用的实体。出于性能原因,我需要通过仅使用这些表中的一部分字段来创建其他几个表的轻量级实体模型,其中一些表有 40 多个字段。

这些新创建的轻量级实体也指向与重量级模型相同的数据库表。

不幸的是,我现在遇到了上面customer_order_base只有 2 个字段的表格。是否可以创建另一个实体以映射到该表?我只想将它用于阅读目的并与其他表连接。

如果没有这个表,我就无法对某些表进行必要的连接。

Is the first case done like this? Using the 2 columns which already exist in the original mapping?

@Immutable
@Entity("DuplicateCustomerOrderBasename=")
@Table("customer_order_base")
public class DuplicateCustomerOrderBase {

  @Id
  @Generator(...)
  BigInteger cust_id;

  @Column(name="cust_fact", insertable="false", updatable="false")
  private String cust_lookup_fact;

}

标签: hibernatejpa

解决方案


我只知道将同一个表映射到不同实体的两种方法:

第一的:

将整个表映射到一个实体。您可以将更多实体映射到同一个表,但您必须使用org.hibernate.annotations.Immutable. 您不能修改这些小实体。

第二:

将继承与继承策略一起使用InheritanceType.SINGLE_TABLE

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Base {

    @Id
    private Long id;
}

@Entity
@DiscriminatorValue("SubBase")
public class SubBase1 extends Base {

    private String value1;
}

推荐阅读