Assume I have the following repository in which I use derived query methods
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByName(String name);
List<User> findByX(String x)
List<User> findByX(String y)
...
}
Now, I have the requirement to find the users by some complex query that can't be implemented with derived query methods. I could use the @Query annotation, but I do not want to write a bunch of SQL like queries. I'd rather use the CriteriaBuilder.
I can implement the interface in a class and get access to the entity manager, but that obviously requires me to implement the methods defined, as well as all spring repository methods defined. Implementing those methods would require extra effort.
public class UserRepositoryImpl implements UserRepository {
@PersistenceContext
private EntityManager em;
}
I could create an independent UserRepositoryImpl, but then I would have to think about which one I need to inject.
public class UserRepositoryImpl {
@PersistenceContext
private EntityManager em;
}
Is it possible to use derived query methods and the criteriabuilder in a single instance?