hibernate - 我可以创建另一个只有 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;
}
解决方案
我只知道将同一个表映射到不同实体的两种方法:
第一的:
将整个表映射到一个实体。您可以将更多实体映射到同一个表,但您必须使用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;
}
推荐阅读
- c# - 无需用户交互即可登录 Google 服务
- java - WebView 显示
- python - 将标准偏差添加到熊猫 groupby 对象的平均值
- c# - 如何在不将整个项目导入 bin 的情况下使用另一个项目的 Project.Properties?
- shell - 以空格分隔的读取 Shell 脚本选项
- spring-boot-test - 如何修复无法找到 meta.properties 的嵌入式 kafka 的错误
- javascript - 如何将带有图像和所有样式的 HTML 页面转换为 PDF 然后下载?
- python - 浏览文件并在不同文件中使用其路径
- c# - 尝试创建表并插入数据库
- r - 根据另一列值修改列值