Nov 20th, 2012, 03:31 PM
Best practice to use QueryDSL with Spring data?
I'm new to Spring data JPA and am trying to understand how to best use it with QueryDSL. Without QueryDSL, I know I can easily create any queries in my repos using @Query annotations. That keeps all my queries together at the repo layer where they should be.
However, I'm having trouble understanding the correct practice for using QueryDSL. From what I can see, I need to either create my own custom repository implementation and have my repo interface extend my custom implementation interface or put all my QueryDSL queries at a service layer which wraps my repo.
In the custom repo case, I lose the support of the QueryDSL in Spring Data (since I don't have access to the SD repo in my custom repo) and have to use the EntityManager to create my queries/etc. I cannot use methods that support QueryDSL like findAll(QueryDSL predicate), etc.
So the only way I can make use of QueryDSL support in SpringData is to put my QueryDSL logic at a service layer which can then use the Repo. But that does not sound architecturally correct to me - essentially, I am moving my query logic out of my repo layer to my service layer, where it doesn't belong.
Neither solution sounds particularly attractive to me. Is there a 3rd way that is more appropriate? Or am I misunderstanding how to properly use QueryDSL and Spring Data? Is there a particular reason why this design was chosen?
Tags for this Thread