I am using spring boot reactive and Couchbase
Flux<Item> findByLocation_LocationIdOrderByCreatedAtDesc(String locationId);
Just to point here I am accessing Location.locationId. Location object has locationId field in main document
Also below properties is not printing the generated Couchbase query, if anyone can help with that too,
logging:
level:
org.springframework.data: DEBUG
The response is fine when database has only one entry, but when the db has more than one entry matching criteria it throws "Source emitted more than one item" .
My controller is returning
Flux<Item>
Following two annotations are mandatory or it keep returning the exception
#N1qlPrimaryIndexed
#ViewIndexed
#Ghrissology I already had ReactiveCouchbaseRepository
Normally, the spring Data change the query based on the return type.
if you want one result it use getSingleResult() automatically :
Item findByLocation_LocationIdOrderByCreatedAtDesc(String locationId);
in you case, i think he didn't understand the return type because you didn't implement a ReactiveCrudRepository:
public interface ReactiveItemRepository
extends ReactiveCrudRepository<item, String> {}
Related
dears.
Currently, I am working with JpaSpecificationExecutor API, cause I need to create dynamic queries based on a lot of optional search criteria, for this I am starting with a root entity, which is included in the generic type in the JpaSpecificationExecutor interface.
The code to make the build in the Specification (which is included in the findAll method that belongs to repository interface) is the following:
public Specification<T> build(){
return (root, query, criteriaBuilder) -> {
Predicate filters = null;
//createPredicate is a method to create predicates (it does not matter in this code)
filters = createPredicate(root, criteriaBuilder, searchCriterias);
return filters;
};
}
Now each is working fine, but there is a little detail, in the query I am using joins to make relation with other entities, but when I get the response, like in this code:
List<EXAMPLE> examples = repository.findAll(filters.build());
I am not able to see the filter data in other entities, just I can see the field in the root entity (It is EXAMPLE entity) because these ones are appearing in the query formed. so, I would like to include in the SELECT the other entities, in order to see the filter data by means of the JOINs, but until now I have not been able to do it.
I have seen in any inputs, that I can use these codes in my build code, I have tried, but the new files are not appearing in the generated query:
query.select, or query.multiselect
Ref:
Ref. 1
Also, I have found that this is not working with JpaSpecificationExecutor, because the list of attributes in the SELECT is assigned by the entity Ref. 2
My question is, does someone know if exists another way to change the field in the SELECT with JpaSpecificationExecutor.
Regards.
I implemented a query where i mined data from a database but i have to change it so i mine my data from a custom function in my code. I read the documentation and added the annotation import on the configuration. The query throws that error:
Failed to resolve event type, named window or table by name 'path.to.my.class.customfunction'
I don't know the type that my function have to return but i tried Arraylist and Hashmaps with key an integer and value a custom class and didn't work.
My final query want to look like this :
select * from LocationEvent as loc,
***CustomFuntion()*** as product
where loc.id=product.id ;
I kept the structure i used for database connection. I don't know if there is another way to solve this. Thanks.
EDIT: I managed to make call the custom function with that query :
select path.to.class.getProducts() as product from pattern[every timer:interval(3 sec)]
My function right now return an ArrayList and the query returns this:
[Product{ProductID=124,.....,},Product{...}]
So now my problem is that i can't access properties of Product on the query like products.ProductID
If you want to have a custom function in the from-clause you can use the "method:". The docs have this described here: Accessing Non-Relational Data via Method. The Esper runtime then calls your method to get events/rows.
I'm doing some junit tests to improve my application. One of these, tests the deletion of a single raw (indicated by an id as primary key) if present, and this works fine. Now I'm testing how my application behave if I want to delete an id Not present in my database.
What I expect is that my test passes with 0 rows affected, but he doesn't pass giving me this error:
No class com.package1.package2.package3.entities.className entity with id 326L exists!
Some advice?
deleteById() from CrudRepository firstly tries to find entity by Id.
In case no entity is found it throws exception; You can have your own repository and declare deleteAllByIdIn() method which takes collection of ids as argument and ORM will create its implementation for you.
This way you should not get any exceptions even if entities with such ids were not present. Or you can always make a native SQL query that deletes the row in DB by id.
SOLUTION:
The method should be :
#Query (your query, nativeQuery=true)
#Modifying
#Transactional
void DeleteById(#Param(id) Long id)
For my application I have to perform a custom count on elastic, I want to use the #Query annotation for this in the ElasticsearchCrudRepository we use. When I use the following signature:
#Query("CUSTOM BOOL QUERY HERE")
long countItemsCustom();
This leads to a java.lang.IllegalArgumentException: Expected 1 but found 30 results Exception since it is executed as an query instead of an count. For spring-data-cassandra we have a special #CountQuery annotation to solve this issue. Is there a similar solution in spring-data-elasticsearch ?
I could use the elastic client or template to perform a custom query and get the results, but I prefer using the existing repository interface for this if possible.
No, that's not possible at the moment. The ElasticsearchStringQuery class checks the return type of the method that is annotated with the #Query annotation and then executes the ElasticsearchOperations.queryFor...() method that is appropriate for the method's return type. These are queries for data and not for the count.
I created an issue in the Spring Data Elasticsearch Jira to have this feature added.
I'm using Spring Data REST 2.1.4.RELEASE.
I created
an entity Booking,
its REST repository (extending CrudRepository) named BookingRepository
and a projection BookingDetails (annotated with #Projection(name="details", types = Booking.class)) for returning some of its linked entities exploded, such as Resource, Activity, Applicant etc.
The client gets all bookings with .../rest/bookings and the JSON response includes links for the linked entities. If it adds ?projection=details then the linked entities are exploded and returned. And this is great.
Now I add this custom method to the repository:
List<Booking> findByApplicant(#Param("applicant") Person applicant);
When the client invokes it with .../rest/bookings/search/findByApplicant?applicant=5, there seem to be no way to request the details projection. Following attempts are ignored:
adding &projection=details to the query string
making the method always return BookingDetails:
List<BookingDetails> findByApplicant(#Param("applicant") Person applicant);
Summarizing, custom search methods (findBy*) never return a projection. Unless you annotate the repository with #RepositoryRestResource(excerptProjection = BookingDetails.class), but this leads to some problems, first of all the client has to always use the same projection. How can we allow the user to use projections also with findBy* methods?
I verfied this working with Spring Data REST 2.2.1, so please update it. Make sure your client actually sends the the requested parameters as you intend. While debugging, I found out that e.g. cURL drops query parameters if you do not explicitly quote the URI. So this:
curl http://localhost:8080/orders/search/findByApplicant?applicant=5&projection=details
will not send any of the query parameters. Once you quote the URI, it will.
curl 'http://localhost:8080/orders/search/findByApplicant?applicant=5&projection=details'
Sort of the same is in place for the increasingly popular HTTPie. With it the required syntax is:
http :8080/orders/search/findByApplicant applicant==5 projection==details
In case you can't get it to work that way, it would be cool to get a running example project to look at.