Using Spring Data nad Querydsl we can just declare repository interface and skip the implementation class. Some methods with a specific name or using @Query annotation and that's all.
But sometimes I'd like to use JPAQuery and define method's body by myself, let's say
@Repository
public class MyRepositoryImpl implements MyRepository {
@PersistenceContext
private EntityManager em;
@Override
public List<Tuple> someMethod(String arg) {
JPAQuery query = new JPAQuery(em);
...
}
but this way I would have to implement other MyRepository interface methods, which ruins all Spring Data's advantages!
I can see two options:
- Declare another interface per each repository and then normally implement it (which doubles number of interfaces)
- Inject EntityManager into @Service class and implement my custom methods there
I like option #2 more, but as far I as know, in @Service class we should only call repository methods, so it's not a perfect solution as well.
So how does programmers deal with it?