java - spring-data-rest 和微服务:具有@OneToOne 关系的实体与另一个 spring-data-rest 服务中的实体
问题描述
在我的 spring-data-rest 应用程序中,我有以下实体
@Entity
public class com.foo.client.Foo {
@Id
@Column(name = "id", nullable = false, length = 48)
public String id = UUID.randomUUID().toString();
@OneToOne
public Bar bar;
}
@Entity
public class Bar {
@Id
@Column(name = "id", nullable = false, length = 48)
public String id = UUID.randomUUID().toString();
public String name;
}
我为每个实体类都有 JpaRepository:
@RepositoryRestResource(collectionResourceRel = "foos", path = "foos")
public interface FooRepository extends JpaRepository<Foo, String> {
}
@RepositoryRestResource(collectionResourceRel = "bars", path = "bars")
public interface BarRepository extends JpaRepository<Bar, String> {
}
我使用以下方法为 Foo 和 Bar 创建了一个实例:
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:foo:test:0"}' http://localhost:8000/foos
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:bar:test:0", "name" : "0"}' http://localhost:8000/bars
然后,我使用以下方法将 Bar 实例关联到 Foo 实例:
curl -i -X PUT -d "http://localhost:8000/bars/urn:bar:test:0\n"
-H "Content-Type:text/uri-list" "http://localhost:8000/foos/urn:foo:test:0/bar"
当这两个实体在同一个 spring-data-rest 服务端点中定义时,一切都很好,我可以使用以下方法获取与 foo 实例关联的 bar 实例:
curl -i -X GET -H "Content-Type:application/json" "http://localhost:8000/foos/urn:foo:test:0/bar"
问题:查看存储实体的 postgresdb,我看到一个关联表 FOO_BAR,其中有两列保存每个实体的 id。但是,我看不到 bar 的 URL 存储在哪里,我想了解它的存储位置。
现在,如果我将我的应用程序拆分为两个单独的 spring-data-rest 服务,一个 foo-service 用于 Foo,另一个 bar-service 用于 bar 在不同的端口,并且还在两个项目之间拆分 Repository 类,然后创建关联不起作用和我得到一个 404。请参阅下面的修改代码:
我使用以下方法为 Foo 和 Bar 创建了一个实例:
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:foo:test:0"}' http://localhost:8000/foos
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:bar:test:0", "name" : "0"}' http://localhost:8001/bars
然后,我使用以下方法将 Bar 实例关联到 Foo 实例:
curl -i -X PUT -d "http://localhost:8001/bars/urn:bar:test:0\n"
-H "Content-Type:text/uri-list" "http://localhost:8000/foos/urn:foo:test:0/bar"
当 Foo 和 Bar 由不同的 spring-data-rest 服务管理时,上面的最后一个请求给了我一个 404。
我怎样才能让第二个案例工作?
请注意,我在示例中使用了这个优秀的资源。
解决方案
It turns out that when the code was split across two separate services (separate modules in same multi-project gradle) I also need the service with the owner entity (foo-service) to have a BarRepository for the owned Entity (Bar). When I copy the BarRepository from bar-service module to foo-service module then I no longer get a 404 and the relationship between Foo to Bar can be observed in the API. Of course this only works if foo-service and bar-service share the same database (which at present in my case they do).
I would be interested in an answer that describes how to solve this problem in a general case where the two services do not share a database.
推荐阅读
- python - 如何使用递归创建二维列表而不将其作为参数传递给递归函数,python
- javascript - React 16.6 惰性/悬念 DOM 异常
- python - 准确获取跨越不同年份的两个日期之间的月数的最佳方法
- python - 转换为十六进制字符串时出现错误“需要整数”?
- vim - 从多个文件grep,在vim中交互修改匹配行的内容,保存后,更改应保存到相应文件
- mongodb - MongoDB:最新的 docker 容器以退出代码 14 终止
- python - pip安装,我应该如何在windows上添加include和lib目录?
- cassandra - Cassandra 节点在批量插入时失败
- java - 如何在 spring application.properites 中注入包含 ${...} 的字符串
- android - Libgdx-音乐不会停止