public interface UserRepo extends JpaRepository<User, Long> {
public List<User> findById(long id);
public List<User> findByEmail(String email);
public List<User> findByEmailAndCode(String email, Code code);
public List<User> findByEmailAndClassType(String email, ClassType code);
}
public class UserService {
@Autowired
UserRepo userRepo;
public List<user> fetchByClassType(ClassType ct) {
return userRepo.findByEmailAndClassType("email", ct);
}
}
here email need to fetch once how to avoid it many time to go on database or any other solution in controller it need to give again and again in each request mapping ...suggest




Вместо того, чтобы создавать кучу похожих методов репозитория, вы можете использовать технические характеристики.
См. Документ здесь: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#specifications
Вам необходимо расширить JpaSecificationExecutor:
public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor {
...
}
Затем вы получите метод findAll (), который принимает спецификацию.
Создайте спецификацию, например:
public static Specification<Customer> isLongTermCustomer() {
return new Specification<Customer>() {
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query,
CriteriaBuilder builder) {
LocalDate date = new LocalDate().minusYears(2);
return builder.lessThan(root.get(_Customer.createdAt), date);
}
};
}
Или, если вам не нравится API критериев JPA, вы также можете использовать QueryDSL:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.extensions.querydsl
но дело в том, что если findBySubject (role, subject) findByClassType (role, classtype) и т.д. здесь каждый раз, когда требуются роли, в этом случае, какой удобный способ, пожалуйста, предложите
его красивое решение помогает в поиске