Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Domain Model is coupled with the JPA technique. #148

Open
c5ms opened this issue Nov 21, 2024 · 2 comments
Open

The Domain Model is coupled with the JPA technique. #148

c5ms opened this issue Nov 21, 2024 · 2 comments

Comments

@c5ms
Copy link
Contributor

c5ms commented Nov 21, 2024

I found the domain model is marked with JPA annotations, but the repository is not directly implemented by JPA.
So I think the reason the repository is a plain java interface is that we can choose the repository technique by the application, we say the application provides the actually runtime and the underlaying implementation, it can be see as an runtime.
But if the repository is not implemented inner the domain layer, the domain entity should not coupled with any concrete technique

public class Cargo implements Entity<Cargo> {

  public long id;

  public String trackingId;
}

public interface CargoRepository {
  void store(Cargo cargo);
}


public class JPACargoRepository  implements CargoRepository{

  public void  store(Cargo cargo){
       JPACargo cargo = CargoConvertor.toJPACargo(cargo);
       this.emtityManager.persist(cargo);
   }

}

If so, wen can freely change the persistence layer from RDBMS likes mysql to NoSQL like mongodb without affecting the core domain. Even we can move the core domain layer into an single jar.

@orende
Copy link
Contributor

orende commented Nov 21, 2024

Indeed, me and @mackapappa have discussed this for quite some time. The only reason the JPA annotations have been placed in the domain objects is because we wanted to move away from using XML bindings for Hibernate, and this was the recommended way to migrate. However, just like you say, this causes the domain objects to be coupled to the repository implementation, which is not ideal.

We do actually have two branches that demonstrate two alternative ways of implementing the repository layer without coupling the domain objects and repository implementation:

@c5ms
Copy link
Contributor Author

c5ms commented Nov 21, 2024

@orende
Thanks, I'm just thinking to fix it, aha.
BTW, Could you help to review my PR for upgrade to spring boot 3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants