I tried to use spring pageable with Where clause and containing but it returns nothing.
My Spring version : 2.1.4.RELEASE
Database : MySql
public interface ItemRepository extends JpaRepository<ItemEntity, String> {
List<ItemEntity> findByCodeIgnoreCaseContainingAndNameIgnoreCaseContaining(String code, String name);
}
Either Change the JPA Query as below :
Page<User> findByCodeIgnoreCaseContainingAndNameIgnoreCaseContaining(String code, String name,Pageable pageable);
Or use PagingAndSortingRepository instead of JpaRepository.
Please refer https://www.baeldung.com/spring-data-jpa-pagination-sorting
Related
I'm using Spring Boot with ElasticsearchRepository and RestHighLevelClient. I have this query in mt repository class:
public interface BillingCycleRepository extends ElasticsearchRepository<BillingCycle, String> {
List<BillingCycle> findByUserIdOrderByStartDateDesc(String userId);
}
In my console, I can see this:
org.elasticsearch.client.RestClient : request [POST http://localhost:9200/dev-billing-cycle/_doc/_search?rest_total_hits_as_int=true&typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512]
It generates a lot of parameters, like rest_total_hits_as_int and `typed_keys.
Is it possible to configure these parameters and add any others, like "filter_path", "preference"?
I'm working on a POC, using Spring Boot 2.0.5, Spring Data JPA using Hibernate. I'm trying to implement a way to stream the result sets for a custom criteria. I have seen examples like
public interface MyRepository implements JPARepository<Person,Long>{
#Query("select p from person p")
Stream<Person> findAll();
}
However I'm extending SimpleJPARepository and want to get results as a stream using a Criteria something like
Stream<Person> findAll(Criteria criteria);
Since I'm using class that extends SimpleJPARepository, I need to provide my implementation. But are there any methods in SimpleJPARepository or its parent classes, that can provide me default implementation using the criteria I provide. Any reference to such example is much helpful.
Also, in some examples I see that #NoRepositoryBean is used and in some cases #Repository. I'm confused between these two and which one should I use and why?
As per Spring Data JPA specifications Spring Data JPA, this is how you can create Criteria queries.
Step 1: extend your repository interface with the JpaSpecificationExecutor interface, as follows:
public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor {
…
}
Step 2: the findAll method returns all entities that match the specification, as shown in the following example:
List<T> findAll(Specification<T> spec);
Step 3: The Specification interface is defined as follows:
public interface Specification<T> {
Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
CriteriaBuilder builder);
}
I have simple rest repository:
TestRepository extends MongoRepository<Test, String> {
#PreAuthorize("#helper.hasPermissionToProjection(#projection)")
#Override
Page<Test> findAll(Pageable pageable);
}
What I need is to pass projection param from the link api/tests?projection=full to my bean's hasPermissionToProjection method.
Is it possible to do like that?
Is it possible to use named parameters for a #Query method in a mongodb repository, just like we can do with a jpa repository (http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html section 2.3.5)?
As an example, I would like to use the following code:
#Query("{'store' : :store, 'app' : :app }")
List<T> findByStoreAndApp(#Param("store") String store, #Param("app") String app);
instead of:
#Query("{'store' : ?0, 'app' : ?1 }")
List<T> findByStoreAndApp(String store, String app);
It is possible, try:
#Query("{'store' : :#{#store}, 'app' : :#{#app} }")
List<T> findByStoreAndApp(#Param("store") String store, #Param("app") String app);
The support of SpEL expressions in #Query was introduced in Spring Data MongoDB 1.8.
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.aggregation.projection.expressions
I'm afraid that the spring boot does provide this function in its source code.
You can create an interface and extends MongoRepository
It provides some simple method, and you don't need the implement it.
Just like use the JpaRepository.
#NoRepositoryBean
public interface MongoRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
<S extends T> List<S> save(Iterable<S> var1);
List<T> findAll();
List<T> findAll(Sort var1);
<S extends T> S insert(S var1);
<S extends T> List<S> insert(Iterable<S> var1);
}
However, if you need to do some special query. You could just #Autowired the MongoTemplate and use its own method. And for redis, springboot even doesn't provide the Repository like MongoRepository. You could just only use the template like StringRedisTemplate for query operation.
May be latter, spring boot would add the same function like JpaRepository for NoSql. But now, it doesn't provide this function.
I'm using spring-data-jpa, and I'm trying to apply spring cache abstraction.
findByEmail() method caches well, however, user variable on retrieve() method at controller which spring-data-jpa provide DomainClassConverter always looks up DB.
In documentation, it calles findOne() to look up the resource, but the #Cacheable trigger won't work.
It seems like implementation class as SimpleJpaRepository just invoke CrudRepository instead of UserRepository which I created and put #Cacheable annotation.
Is there any way to apply #Cacheable to findOne() except for custom DomainClassConverter class ?
UserController.class
#RequestMapping(method = RequestMethod.GET, value = "/users/{user}")
public ResponseEntity retrieve(#PathVariable User user) {
logger.info("Retrieve: " + user);
return new ResponseEntity(user.toUserResponse(), HttpStatus.OK);
}
UserService.class
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
#Cacheable("Users")
User findOne(Long id);
#Cacheable("Users")
User findByEmail(String email);
User findByEmailAndPassword(String email, String password);
Long countByEmail(String email);
}
I've filed and fixed DATACMNS-620 for you.
This issue shouldn't actually occur with Spring Data Commons 1.10.0 M1 (Fowler) as we moved to the Spring Data REST RepositoryInvokerAPI which explicitly checked for custom overrides. I created a fix for the 1.9.x bugfix branch so that we fix this for users of the current release train Evans.
If you want to check this upgrade to Spring Data Commons 1.9.0.BUILD-SNAPSHOT or 1.10.0.BUILD-SNAPSHOT. If you're using Boot, you can simply set a property spring-data-releasetrain.version and set it to either Evans-BUILD-SNAPSHOT or Fowler-BUILD-SNAPSHOT.