[英]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.