简体   繁体   中英

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. I am using an H2 instance for the project.

For the sake of example, let's say I have the same entity class across all services, lets say EntityA . Is it possible to have multiple JPA repositories across all the projects for that EntityA ?

In Service1:

public interface EntityARepository extends JpaRepository<EntityA, ID>

In Service2:

public interface EntityARepository extends JpaRepository<EntityA, ID>

etc...

Having something like that in each of the services, which are each their own spring project? I need to be able to access EntityA across all of the services but would like to only use one H2 instance.

Is this possible? I know that it is not possible to have multiple repositories for the same entity within one spring project. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM