Search code examples
springmodeldomain-driven-designspring-data-jdbc

Only simple models in Spring Data JDBC


Im not sure if i can use Spring Data JDBC also for complex models. My doubts arise especially cause in the Spring Data JDBC (3.0) documentation is written:

"There is a simple model of how to map entities to tables. It probably only works for rather simple cases. If you do not like that, you should code your own strategy. Spring Data JDBC offers only very limited support for customizing the strategy with annotations." https://docs.spring.io/spring-data/jdbc/docs/3.0.0/reference/html/#jdbc.why

I was expecting Spring Data JDBC is also working for more complex cases.


Solution

  • The limitations that rise from this simple model affect mostly legacy projects where you have a database that you maybe can't even change.

    Spring Data JDBC is not intended to map an arbitrary database model to an arbitrary Java domain model, but to use a Domain Driven Design approach and construct the database accordingly.

    But even in the cases where you hit the limitations of Spring Data JDBC you can always fall back on Springs JdbcTemplate without any conflict with the rest of your model which gets persisted by Spring Data JDBC. The same is not true for JPA. Of course you can use JdbcTemplate with JPA as well, but you now have to very different approaches to persistence in your application which can and will interact in interesting ways due to JPA caching and dirty checking.

    I therefore think Spring Data JDBC is an excellent choice for large application and complex models. It's limitations will push you in the direction of better defined smaller modules and less complex models.