I am having a hard time deciding if I should stick with Hibernate for a new project, or get my feet wet with JPA and the new Spring Data implementation.
Is the Spring Data framework intended for large projects or small projects with modest query requirements?
While I certainly see the advantage in code reduction by using the @Query
annotation, what do you do for dynamic queries? What about when you want to implement a save() method that's quite complex?
The documentation says to make a Custom interface and implementation that your main repository implements, but what if you need to access any super methods on the crud repository itself? The crud repository implements the custom one – not the other way around. It seems like an odd design.
I am very uncertain whether this framework will meet the challenges of complex and large applications. I've never ran into many problems with Hibernate, and I'm considering sticking with the good old reliable rather than go with Spring Data JPA.
What should I do? What unforeseen complications and costs will I encounter if I go with Spring Data JPA?
Best Answer
So,
spring-data
does some extra magic that helps with complex queries. It is strange at first and you totally skip it in the docs but it is really powerful and useful.It involves creating a custom
Repository
and a custom `RepositoryImpl' and telling Spring where to find it. Here is an example:Configuration class -
point to your still-needed xml configwith annotation pointing to your repositories package (it looks for*Impl
classes automatically now):jpa-repositories.xml - tellSpring
where to find your repositories. Also tellSpring
to look for custom repositories with theCustomImpl
file name:MyObjectRepository
- this is where you can put annotated and unannotated query methods. Note how this repository interface extends theCustom
one:MyObjectRepositoryCustom
- repository methods that are more complex and cannot be handled with a simple query or an annotation:MyObjectRepositoryCustomImpl
- where you actually implement those methods with an autowiredEntityManager
:Amazingly, this all comes together and methods from both interfaces (and the CRUD interface, you implement) all show up when you do:
You will see:
It really does work. And you get one interface for querying.
spring-data
really is ready for a large application. And the more queries you can push into simple or annotation only the better off you are.All of this is documented at the Spring Data Jpa site.
Good luck.