ExampleMatcher match Id from list of Ids - spring

In table I have column Id, I am using ExampleMatcher to match different fields.
Id column - I want to match any item with its id in the list.
If list is : Arrays.asList(1,2, 3); then I expect to return from database rows with ids 1,2,3.
This is what I tried
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnoreNullValues()
.withMatcher("Id", ids::contains);
But it doesn't filter, also I don't see it in log of generated query.
Update:
repository interface extends PagingAndSortingRepository

Related

Spring Data JPA - How do i get a list of specific rows by ids?

I'm trying to get specific rows from the table by ids, but what I have below is not working.
#Query(value = "SELECT * FROM row r where r.row_id = :row_ids", nativeQuery = true)
List<Object> temp(#Param("albumsIds") String row_ids);
row_ids is all the ids separated by an "or" - id:1 or id:2 or id:3
I'm just trying to do select * from row r where r.row_id = id:1, or r.row_id = id:2, or r.row_id = id:3
Does anyone have an idea what the problem is, or is there a better way to do it?
Simply use:
List<Object> findByIdIn(Collection<Integer> ids);
That will automatically setup the derived query from the query method name itself.
Alternatively, if you want to provide the query programmatically, then the query should be:
#Query(value = "SELECT * FROM row R WHERE R.row_id IN :ids", nativeQuery = true)
List<Object> temp(#Param("ids") Collection<Integer> ids);
MySQL lets use use the IN keyword in queries, which let's us provide a CSV for the IDs to parse and returns any records which ID is in the CSV. This obviously can be used on any data, just for this purpose we'll use the IDs as an example.
Using the method above should minimize the risk of SQL injection significantly, as the Java Collection type casting shouldn't allow a user to provide any values that can cause issues.

Problem rendering the result set columns names with interface projection method names - Fetching Multiple Columns from Multiple Tables

I need multiple columns from multiple tables data.
Using Native Query:
#Query(value = "select t1.name as name, t1.phone as phone, t2.address as address, t2.pincode as pincode
from tablet t1, table2 t2
where t1.id=t2.tab1_id", nativeQuery = true)
List<MultipleColumnValues> getMultipleColumnsFromMultipleTables();
The return type of the above Query is a List of below projection:
public interface MultipleColumnValues {
String getName();
String getPhone();
String getAddress();
String getPincode();
}
I am getting the List successfully.
But:
The results are storing in different variables like,
The column names in the query mentioned are not rendering properly with the projection method Names.
eg: column 'name'(db query) value is stored in getPincode(), and pincode(db Query) is stored in getAddress()
How to render/map the result set names with the exact projection method names correctly?
This is a bug in Spring Data JPA version that is used in Spring Boot 1.5.2.
You have to upgrade to 1.5.3 then everything will work.
That't link to the issue: https://jira.spring.io/browse/DATACMNS-927

2sxc - Get Calendar data

I have Entity "News" with many fields and entity "EventTime" which contains field "DateTime"
In "News" there is also field for List of "EventTime"
I am able to filter and sort "News" on many fields of "News" entity...
var data = AsDynamic(App.Data["News"]);
data = data.Where(x => ....);
data = data.OrderBy(x => ...);
But I don't know how to make linq query to get list of "News" for all "EventTime"..
eg.:
if I have this data:
News[0]-Title1 with EventTime[0]-2019/05/01 and EventTime[1]-2019/06/02
News[1]-Title2 with EventTime[0]-2019/05/10 and EventTime[1]-2019/06/10
I want to get list of "News" like this:
Title1 - 2019/05/01
Title2 - 2019/05/10
Title1 - 2019/06/02
Title2 - 2019/06/10
How to get this?
This seems to mostly be a LINQ question, basically you want to have a result where the original items occur more than once, and the final sort uses multiple possible values. I recommend to something along these lines:
create a list of all items with their new date, do this using Linq SelectMany and in that, do an newsItem.Select return something like a new { Date = the-date-you-need, Item = the-as-dynamic }.
Now OrderBy the Date
That's basically it :)

spring data jpa query select all rows if given parameter collection is null

I want to write a query that selects all rows by given type collection for my fileRepository(There are 3 types as "1", "2" and "3"), if that parameter is not given from frontend, then it should return all rows.
I am using a Repository with #RepositoryRestResource annotation to achieve this.
It works fine for single type query(not Collection)
#Query("SELECT t FROM #{#entityName} t WHERE :fileType IS NULL OR t.fileType = :fileType")
Page<MyFile> findByFileType(#Param("fileType") String fileType, #Param("page") Pageable pageable);
it returns all entities when I don't give any arguments i.e : "http://localhost:8080/api/myFiles/search/findByFileType"
and returns all Files which have type "1" when I search with 1 parameter:
"http://localhost:8080/api/myFiles/search/findByFileType?fileType=1"
But problem occurs when I try to write a query from collection of types and I tried this
#Query("SELECT t FROM #{#entityName} t WHERE :fileTypes IS NULL OR t.fileType IN :fileTypes")
Page<MyFile> findByFileType(#Param("fileTypes") Collection<String> fileTypes, #Param("page") Pageable pageable);
It still works for the example localhost links that I gave above but multiple parameter throws "java.sql.SQLSyntaxErrorException: ORA-00920: invalid relational operator" error with this link:
"http://localhost:8080/api/myFiles/search/findByFileType?fileType=1,2"
Edit: I am using Oracle11g as database.

Laravel query builder - Select elements unique or null on specific column

I have a model Form for table forms. There is a column called guid which can be null, or contain some sort of grouping random hash.
I need to select all forms that have column guid either null or unique in current search. In other words, for repeating guid values in current search I select only first occurence of every guid hash.
I tried:
$results = App\Form::where(... some where clauses .. ).groupBy('guid')
and it's almost ok, but for all rows, where guid == NULL it groups them and selects only one (and I need all of them).
How can I get the unique or null rows either by building proper SQL query or filtering the results in PHP?
Note: I need my $results to be an Illuminate\Database\Eloquent\Builder instance
EDIT:
I fount out that SQL version of query I need is:
SELECT * FROM `forms` WHERE .... GROUP BY IFNULL(guid, id)
What would be equivallent query for Laravel's database query builder?
UPDATE: Using DB::raw
App\Form::where(... conditions ...)
->groupBy(DB::raw("IFNULL('guid', 'id')"));
Or the another way could be:
You can also use whereNotNull, whereNull & at last merge both the collections using merge() like this:
First get the results where guid is grouped by (excluding null guid's here):
$unique_guid_without_null = App\Form::whereNotNull('guid')->groupBy('guid')->get();
Now, get the results where guid is null:
$all_guid_with_null = App\Form::whereNull('guid')->get();
and at last merge both the collections using merge() method:
$filtered_collection = $unique_guid_without_null->merge($all_guid_with_null);
Hope this helps!
For your edited question, you can use raw() as;
->groupBy(DB::raw("IFNULL('guid', 'id')"))
So your final query will be as:
$results = App\Form::where(... some where clauses .. )
->groupBy(DB::raw("IFNULL('guid', 'id')"));
By above query, your $results will be an instance of Illuminate\Database\Eloquent\Builder.

Resources