Home > Back-end >  Should I create a new repository for each table?
Should I create a new repository for each table?

Time:02-19

I am new in Spring Data JPA and wondering if I should create a new repository for each table.

For example, I have a Product table and I created ProductRepository, then I create a related entity ProductStock that also keeps UUID values of Product and Brand tables. So, in this case should I create a new repository for this ProductStock? Or should I create necessary methods in ProductRepository?

Is there any proper approach for this in Spring Data JPA?

CodePudding user response:

As far as I know, you should have a JPA repository for each individual entity. I can't answer this question based on facts or best practices, but we are using one JPA Repository for every different entity stored in the database. And we then associate them in the business logic for special use cases.

In your case, I would recommend either (when possible) relations on data structure level and if not possible just work with the queried entities in the ProductService as you wish. To fetch data or write @Query 's use the JPA Repository for the queried table ProductStock repository and Product repository.

  • Related