为什么实体-值关系在 Spring Data JDBC 中实现为反向引用 使用地图让它成为真正的价值

如何解决为什么实体-值关系在 Spring Data JDBC 中实现为反向引用 使用地图让它成为真正的价值

在 Spring Data JDBC 中,如果一个实体(例如 Customer)具有一个值(例如 Address),如示例中的 here,则该值具有反向引用列(表 address 中的列 customer)到数据库架构中的实体:

CREATE TABLE "customer" (
  "id"                  BIGSERIAL       NOT NULL,"name"                VARCHAR(255)    NOT NULL,PRIMARY KEY (id)
);

CREATE TABLE "address" (
  "customer"            BIGINT,"city"                VARCHAR(255)    NOT NULL
);

这样做的问题是,如果您在一个实体中或什至在不同的实体中多次使用该 Address 值,您必须为每次使用定义一个额外的列。只有实体的主 ID 存储在这些列中,否则无法区分它是哪个实体。在我的实际实现中,我有五个用于 Address 值的列:

  "order_address"            BIGINT,-- backreference for orderAddress to customer id
  "service_address"          BIGINT,-- backreference for serviceAddress to customer id
  "delivery_address"         BIGINT,-- backreference for deliveryAddress to customer id
  "installation_address"     BIGINT,-- backreference for installationAddress to provider_change id
  "account_address"          BIGINT,-- backreference for accountAddress to payment id

我理解它是如何工作的,但我不理解这个返回参考实现背后的想法。那么有人可以对这个问题有所了解吗?谢谢!

解决方法

至于大多数好问题,答案有很多方面。

历史/对称性答案

当涉及实体之间的引用时,Spring Data JDBC 支持 1:1(您询问的那个)和 1:N(列表、集合和映射)。 对于后者,除了反向引用之外的任何东西都是奇怪的/错误的。 并且使用 1:1 的反向引用变得基本相同,简化了代码,这是一件好事。

DML 流程答案

通过反向引用,插入和删除的过程变得更加容易:首先插入聚合根(在您的示例中为 customer),然后是所有引用的实体。如果这些实体有更多实体,它会继续工作。删除工作相反,但同样直接。

依赖答案

聚合中的引用实体只能作为该聚合的一部分存在。从这个意义上说,它们取决于聚合根。没有那个聚合根就没有内部实体,而聚合根通常也可能在没有内部实体的情况下存在。因此,内部实体携带引用是有道理的。

ID 答案

通过这种设计,内部实体甚至不需要 id。它的身份完全由聚合根的身份给出,如果与同一实体类存在多个一对一关系,则使用反向引用列。

替代方案

所有的原因或多或少都基于单一的一对一关系。我当然同意,对于同一个班级的两个这样的关系看起来有点奇怪,而在您的示例中使用 5 就变得荒谬了。在这种情况下,您可能需要寻找替代方案:

使用地图

不要像这样为您的 Customer 类建模:

class Customer {
  @Id
  Long id;
  String name;
  Address orderAddress
  Address serviceAddress
  Address deliveryAddress
  Address installationAddress
  Address accountAddress
}

使用这样的地图

class Customer {
  @Id
  Long id;
  String name;
  Map<String,Address> addresses
}

这会导致像这样的 address

CREATE TABLE "address" (
  "customer"            BIGINT,"customer_key"        VARCHAR(20).    NOT NULL,"city"                VARCHAR(255)    NOT NULL
);

您可以使用 @MappedCollection 注释控制列名称,如果需要,您可以为单个地址添加临时 getter 和 setter。

让它成为真正的价值

您将 Address 称为 ,而我将其称为 实体。如果它应该被视为一个值,我认为你应该将它映射为 embedded 像这样

class Customer {
  @Id
  Long id;
  String name;
  @Embedded(onEmpty = USE_NULL,prefix="order_")
  Address orderAddress
  @Embedded(onEmpty = USE_NULL,prefix="service_")
  Address serviceAddress
  @Embedded(onEmpty = USE_NULL,prefix="delivery_")
  Address deliveryAddress
  @Embedded(onEmpty = USE_NULL,prefix="installation_")
  Address installationAddress
  @Embedded(onEmpty = USE_NULL,prefix="account_")
  Address accountAddress
}

这会使 address 表变得多余,因为数据将被折叠到 customer 表中:

CREATE TABLE "customer" (
  "id"                  BIGSERIAL       NOT NULL,"name"                VARCHAR(255)    NOT NULL,"order_city"          VARCHAR(255)    NOT NULL,"service_city"        VARCHAR(255)    NOT NULL,"deliver_city"        VARCHAR(255)    NOT NULL,"installation_city"   VARCHAR(255)    NOT NULL,"account_city"        VARCHAR(255)    NOT NULL,PRIMARY KEY (id)
);

还是聚合?

但也许您需要自己的地址,而不是作为客户的一部分。 如果是这种情况,地址就是它自己的聚合。 聚合之间的引用应建模为 id 或 AggregateReference。这在 Spring Data JDBC,References,and Aggregates

中有更详细的描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res