简体   繁体   English

跨不同 spring 项目的同一实体的多个 JPA 存储库

[英]Multiple JPA repositories to same entity across different spring projects

I am working on a pretty complex project that involves multiple services in the backend.我正在从事一个非常复杂的项目,该项目涉及后端的多个服务。 Each service is its own spring project.每个服务都是它自己的 spring 项目。 I am using an H2 instance for the project.我正在为该项目使用 H2 实例。

For the sake of example, let's say I have the same entity class across all services, lets say EntityA .例如,假设我在所有服务中都有相同的实体 class ,比如说EntityA Is it possible to have multiple JPA repositories across all the projects for that EntityA ?是否可以在该 EntityA 的所有项目中拥有多个EntityA存储库?

In Service1:在服务 1 中:

public interface EntityARepository extends JpaRepository<EntityA, ID>

In Service2:在服务 2 中:

public interface EntityARepository extends JpaRepository<EntityA, ID>

etc... ETC...

Having something like that in each of the services, which are each their own spring project?在每个服务中都有类似的东西,每个服务都是他们自己的 spring 项目? I need to be able to access EntityA across all of the services but would like to only use one H2 instance.我需要能够跨所有服务访问EntityA ,但只想使用一个 H2 实例。

Is this possible?这可能吗? I know that it is not possible to have multiple repositories for the same entity within one spring project.我知道不可能在一个 spring 项目中为同一实体拥有多个存储库。 But how about across multiple?但是跨多个呢?

As it was already said in the comments: Yes you can do that as long as the repositories are in different projects.正如评论中已经说过的那样:是的,只要存储库位于不同的项目中,您就可以这样做。

If multiple projects actually write to the database you might want to ensure you don't get "lost updates" by using JPAs optimistic locking facility .如果多个项目实际写入数据库,您可能希望通过使用JPA 乐观锁定设施来确保不会“丢失更新”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring JPA – 具有相同存储库的多个数据库 - Spring JPA – Multiple Databases with the same Repositories 同一实体的两个Spring存储库-不同的数据源 - Two Spring Repositories for same entity - Different Datasources Spring JPA 存储库与 Stream<entity></entity> - Spring JPA Repositories with Stream<Entity> Spring Data JPA:从相同的存储库接口定义实例化多个存储库,每个存储库指向不同的数据库 - Spring Data JPA: Instantiate multiple repositories from same repository interface definitions, each pointing to a different database 与同一实体的Spring JPA多重关系 - Spring JPA Multiple Relations with same entity 将Spring JPA Specification应用于多个存储库和查询 - Apply Spring JPA Specification to multiple repositories and queries 如何跨不同存储库中的 gradle 个项目共享插件部分 - How to share plugins section across gradle projects in different repositories 针对同一模式的多个JPA存储库 - multiple JPA repositories against same schema Spring Boot:使用dblink在多个数据库中对同一实体建模 - Spring Boot: modeling same entity across multiple databases with dblink Spring jpa hibernate 如何将关系映射到同一实体的多个对象 - Spring jpa hibernate how to map a relationship to multiple objects of the same entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM