Spring Slice Specification Pagable - spring

I want to use Slice with Specification and Pagable; not List or Page.
The following is working in a JpaRepository:
public interface PersonRepository extends JpaRepository<Person, UUID>,
JpaSpecificationExecutor<Person> {
Slice<Person> findAllBy(Pageable pageable);
}
But if i add a Specification at least 1 parameter is provided and it will be failed:
Slice<Person> findAllBy(Specification<Person> specification, Pageable pageable);
java.lang.IllegalArgumentException: At least 1 parameter(s) provided but only 0 parameter(s) present in query.

This is currently not supported by Spring Data JPA. See https://github.com/spring-projects/spring-data-jpa/issues/1311
You'd have to create a custom method to achieve this.

Related

Returning only the first 10 record - Redis OM

I’m using Redis OM for spring boot, I am having trouble querying objects because it only returns the first 10 records.
Repository Class:
public interface RedisBillerRepository extends RedisDocumentRepository<Biller, Long> {
List<Biller> findByClientIds(String clientId);
}
Is there a way to return ALL the objects with the specific clientId? Not the first 10 only.
The only way which i found was with the interface Page. For example your Repository would look like this:
public interface RedisBillerRepository extends RedisDocumentRepository<Biller, Long> {
Page<Biller> findByClientIds(String clientId, Pageable pageable);
}
And your class could look like this
public class BillerService {
#Autowired
RedisBillerRepository redisBillerRepository;
public List<Biller> getAllClientsById(String clientId){
Pageable pageRequest = PageRequest.of(0, 500000);
Page<Biller> foundBillers = redisBillerRepository.findByClientIds(clientId, pageRequest);
List<Biller> billersAsList = foundBillers.getContent();
return billersAsList;
}
}
You have to set the limit for now.
I'm the author of the library... #member2 is correct. RediSearch currently has a default for the underlying FT.SEARCH (https://redis.io/commands/ft.search/) method of returning the first 10 records found. To override that, the only way to do so currently is to use the Pagination constructs in Spring.
I will expose a configuration parameter in upcoming versions to set the MAX globally.

Spring boot not able to read custom #Query annotation

I have requirement when we apply #CustomQUery annotation, then I need to intercept this method and append the query predicate which I already know.
Created
#Retention(RetentionPolicy.RUNTIME)
#Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
#Documented
public #interface CustomQuery {
Query query();
}
#Repository
public interface FloorRepository extends JpaRepository<TnFloor, Integer> {
public String query="select distinct tnFloor from TnFloor tnFloor where tnFloor.tnBuilding.buildingId in ?1 ";
#CustomQUery(query=#Query(query))
public List<TnFloor> findByBuildingIds(List<Integer> buildingIds);
}
Here, Spring is unable to read this #CustomQUery because I have not mentioned anywhere to read this annotation.
Is this the correct way to create custom query annotation ?
I am getting below exception on application startup.
Could not create query for public abstract java.util.List
FloorRepository.findByBuildingIds(java.util.List)!
Reason: Failed to create query for method public abstract java.util.List FloorRepository.findByBuildingIds(java.util.List)!
No property buildingIds found for type TnFloor!
Did you mean 'buildingId'?;
nested exception is java.lang.IllegalArgumentException: Failed to c
As other people have already said in comments, I think your way of extending the Query annotation is usefull only if you need to do some things more than just extending it.
If you need some paths to enhance the behavior of the #Query annotation, maybe using #Modifying annotation could get the point, or using #NamedQuery and #NamedNativeQuery annotations too.
If it is a requirement you could not resolve with these other annotations, maybe make some click on them to see how they are declared and raised in the Spring IoC ecosystem using Aspect programming.
The problem here, to my point of view, seems not to be related to your annotation, but a missing property, as the error message has told to you :
No property buildingIds found for type TnFloor! Did you mean 'buildingId'?;
Maybe because of a typo error in your annotation when you are using it, which is not found :
declared as public #interface CustomQuery and used as #CustomQUery(query=#Query(query)). Write the things as they are declared will work better I think.
Did you try using native query feature like that too ?
Query q = em.createNativeQuery("SELECT a.firstname, a.lastname FROM Author a");
// of course, update with your own code.
You can look at what JPA is capable of and switch to native query as I have just added if it is not supported.

How to find all by number that starts with Spring JPA?

MyStore class contains itemNbr Integer. I want to find all where itemNbr starts with xxxxxx
I'm using Spring Repository like this . This is what I tried
#Repository
public interface TestRepository extends CrudRepository<MyStore, Long> {
List<MyStore> findByItemNbrStartsWith(int itemNbr);
}
But this throws an exception
java.lang.IllegalArgumentException: Parameter value [2%] did not match expected type [java.lang.Integer (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
a
With default methods you can't.
However you can write your own #Query something like this,
and then pass the itemNbr as a String.
#query("from MyStore where CAST(itemNbr as text) like CONCAT(:itemNbr, '%')")
List<MyStore> findByItemNbrStringStartsWith(String itemNbr);

How to create custom abstract repository like PagingAndSortingRepository, etc?

For example in my persistence:
public interface SomePersistence extends JpaRepository<SomeClass, String> {};
I can write method like:
#Query("some query")
List<SomeClass> getAllWithSomeParam();
and spring knows to use SimpleJpaRepository class - implementation of JpaRepository.
When i write:
#Query("some query")
Page<SomeClass> getAllWithSomeParam(Pageable page);
spring knows to use implementation of PagingAndSortingRepository.
But now i want to add my own returned type - Cursor<T>
It`s mean i want write:
#Query("some query")
Cursor<SomeClass> anyMethodName();
Then i want to give spring my own repository CursorRepository with its personal CursorRepositoryImpl when i have only one method Cursor<T> findAll()
Can i realize it?

Spring JPA without hibernate

I am not able to understand the below method in Spring -JPA.
#RepositoryRestResource
public interface TestRepository extends JpaRepository<Vehicle, BigInteger>{
public List<Vehicle> findAll(Sort sort);
}
Vehice is a entity or domainobject.
What does the method findAll(Sort sort) do.
Please help me.
This declare the method already in JpaRepository again.
List<T> JpaRepository.findAll(Sort sort);
with T = Vehicle take all vehicles form the database (or NOSQL store) with a the given sorting and returns them.
#RepositoryRestResource is a annotation to publish this as an HATEAOS rest resource.
It allows you to find all vehicles, sorted as specified by the Sort parameter.

Resources