Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • In the question the author refer to Entity class and not an EntityJPA class. So even if you can change the repository (database, file, etc ...) you continuing to depend on Entity and I think it's wrong. The repository must be aware of EntityJPA and not Entity. So I wrote down a new question softwareengineering.stackexchange.com/q/442397/385571 to suggest solution Commented Nov 24, 2022 at 2:54
  • Well, your question/answer is neither right nor wrong. It just omitted this Persistence -> Repository and that's what I tried to illustrate with my snippets. The Persistence layer between Service and Repository seems unnecessary because Repository is already an abstraction. The problem is that spring developers usually tight couple their repositories with Spring Data repositories (for convenience). If you define repositories interfaces yourself, then you don't couple the core to the framework (one of the CA premises). Commented Nov 24, 2022 at 10:13
  • The problem is that spring developers usually tight couple their repositories with Spring Data repositories because JpaRepository define all methods save(), findAll(), etc ... In the case of DBRepository you need to create your owns methods and convert Product to ProductJPARepository But I agree with your answer because it answers the question perfectly, this being just a special case with Spring Commented Nov 24, 2022 at 14:32
  • Not necessarily. I don't use Spring Data anymore for that reason (imposes JPA). I use the abstraction that best serves my needs of decoupling. For example, I might use MyBatisDao or the standard EntityManager. But I pay a tax, I'm reinventing the wheel. Spring Data repositories do many of these things for me, but it imposes JPA and I might not want to go with JPA. I'm a huge fan of MyBatis, so I learnt to decouple my persistence from Spring Data too. Commented Nov 24, 2022 at 16:29
  • 1
    @Laiv Spring Data does not impose JPA. In fact, it's a generic abstraction over a bunch of different persistence technologies. There are a bunch of sub-projects, such as Spring Data JPA, Spring Data Mongo, etc, that you can choose from based on the persistence technology you want. See spring.io/projects/spring-data Commented Apr 19, 2023 at 20:56