Customize query method whose paramter is List in Spring Data JPA - spring

I know Spring Data JPA has offered some query methods which can accept a list as its parameter like List<T> findAllById(Iterable<ID> ids).So how can I customize such methods? Can anyone help?

You can use IN. This accepts a collection of attributes.
List<T> findByIdIn(Collection<ID> ids)

Related

Is it possible to call method from entity in Specification or QueryDSL

Is it possible to call method from Entity in Spring Data Specification or QueryDSL?
I have collection of shapes (few types with different parameters) and every shape has method countArea() in entity class. Is it possible to add parameter to search like "area" and use it as filter?
If not, is there any other solution for this?

Handling filtering by a particular field in JPA Spring Boot

I want to implement a filtering feature based on the properties of an entity that I have stored in my db.
I'm using a JPA query to do so. The problem that I'm facing is concerned with entity manager which requires the class of the object that is required to return.
public List<CountryEntity> getSortedCountries(String field, String type) {
return entityManager.createQuery(GET_ALL_SORTED.concat(field).concat(" " + type), CountryEntity.class).getResultList();
}
When I select only one field, let's say the name of the country, the query returns a String and not an object of type CountryEntity.
What is the best approach to handle this problem? Should I create classes for every single case or is there another way that I'm missing?

Expose custom query in Spring Boot Rest API with multiple joins

I have an Spring REST Api and a MySQL Database, now I would like to expose the result of an custom query with multiple joins.
I have tried multiple suggestions that I found online but none of them were working for me so far.
What I want to do is something like a read only DTO that has all the fields of my custom query so that in the end I have one api page exposing the DTO data as JSON so my client (Angular) can read the data from there.
I already tried to:
create an #RestController with an injected EntityManager that executes a NativeQuery and then populates the DTO with the returned data but since my DTO is no Entity I get an Hibernate Mapping Exception
create a custom Repository and its Impl but with a similar outcome
place the Query inside an existing #Entity that is part of the Query statement
What am I missing here? Do I have to annotate my DTO maybe? Cuttently it's just a POJO, I think the #Entity annotation is not the right thing here since I don't want a Table created from my DTO.
Fixed it by letting the Query return an Array of type Object and afterwards mapping it to the DTO Constructor.

Dynamic where condition in CrudRepository

I have a query in Spring boot:
I am using CrudRepository where i need to declare method names for all possible combination of where conditions being used in project. Can we declare a method in CrudRepository which can accept dynamic where condition?
Suppose i have a table named "Student" which is having 20 columns. I don't want to define all possible combination of methods names in CrudRepository for those 20 fields. I need a generic method which can accept a json object as parameter and return records based on the where condition passed as json object. Is there any solution or workaround for this?
I think what you are looking for are called Specifications.
These allow for more precise queries.
It is nicely expained here (take a look at 5. Using JPA Specifications).

Same query method and parameters with different return in Spring Data

I want to use projections in order to return less elements for the same queries.
Page<Network> findByIdIn(List<Long> ids);
Page<NetworkSimple> findByIdIn(List<Long> ids);
Since the queries are created using the name of the method, what options do I have to do the same query but with different name ?
I ran into this today, and the accepted answer is actually incorrect; you can change the method name without altering the behavior. According to the Spring Data documentation:
Any text between find (or other introducing keywords) and By is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct to set a distinct flag on the query to be created or Top/First to limit query results.
Thus you can have a method named findByIdIn and another named findNetworkSimpleByIdIn and the two will return the same data (optionally converted to a different form depending on the defined return type).
Spring Data query via method is constructed by convention and you can't change the name and yet expecting a same behavior.
You can try to use #Query annotations which doesn't depend on the method name, or possibly implementing custom DAO using JPAQuery plus FactoryExpression which has the same effect as projections.

Resources