Latest by group query using Spring Boot JPA - spring-boot

I am using Spring Boot 2.5.0 for one of my test projects and need to get some data from one of the transaction tables. I need only latest record per group from this table. This problem has a solution given multiple times on SO, one of which is https://stackoverflow.com/a/1313293/841221.
However, I want to implement the same using Spring Boot Data JPA and not using a native query as using a native query would make me lose database independence.
Can someone please help me?

Related

What is the best way to maintain queries in Spring boot application?

In My Application, Using the below technologies
Spring boot 2.7.x
Cassandra
spring batch 5. x
java 11
As part of this, I need to extract data from the Cassandra database and need to write out the file
so here I need to use queries to fetch data so
just want to know what is the best way to maintain all queries at one place so any query changes come in the future, I shouldn't build the app rather just need to modify the query.
Using a repository class is necessary. If you are using JPA i recommend using a repository for each Entity class. With JDBC it is possible to create a single repository which contains all the queries. To access the query methodes i would use a service class. In this way your code is structured well and maintainable for future changes.

How to set multiple data sources in Spring Data R2DBC

I am using spring data r2dbc in my new project and need to connect multiple data sources like A data source and B data source.
Is there a way to connect multiple data sources using r2dbc?
Could I get an example or a document if there is a way?
My stacks are below:
Spring Boot 2.3.0.M4
Spring WebFlux
Spring Data R2DBC
If you want to use multiple datasources in a single application, check my multi r2dbc connection factories example.
If you need a multi-tenant like feature check this multi-tenancy-r2dbc example.
I solved this issue using AbstractRoutingConnectionFactory you can check Add support for AbstractRoutingConnectionFactory

Teiid Spring Boot #SelectQuery

I have configured 2 different data souces in my Teiid Spring Boot Application and have been following this example:
https://github.com/teiid/teiid-spring-boot/blob/master/docs/UserGuide.adoc
This example demonstrates a union query from Customer table from two different data sources, however, when i run my application the customerrepository retrieves records only from one database and not from the other.
I have uploaded my source code on github:
https://github.com/purnimadas27/TeiidExample1/tree/master/TeiidSpringBootPrj5
Can someone suggest why is my union query not retrieving results as expected, it appears that the customerrepository is bound to only one of the datasource and is retrieving results only from one datasource and it also appears that #SelectQuery is not being considered at all.
Any help is greatly appreciated, thank you!
See this full example here with updated document
Updated: I could not figure out root issue is, this was not occurring with latest code, so wrote a new example with 1.3.1 release of the Teiid Spring Boot. This should work.

Using Spring Data JPA specification to fetch projection interfaces

I am interested in Spring Data JPA projection but I need to use JPA specification. Is it possible to fetch projection interfaces with JPA specification?
As far as I know, this simply dooes not work and is not supposed to work, at least not for now https://jira.spring.io/browse/DATAJPA-1189
A Spring-Data-JPA Enthusiast has created a project that implements projections with specifications. I am using it in my own project without problem. You may check it from here:
https://github.com/pramoth/specification-with-projection
Update for database level projections
There is an outdated comment below.
Is not query optimized. It just map the results after they have been queried
The last version of this library supports database level projections link to the commit.
Update for the last Spring Boot versions
Until this pull request is not merged, to support the last Spring Boot versions, fixed library sources can be copy pasted directly to the project from here
https://github.com/v-ladynev/specification-with-projection-embeded

Update using Spring data JPA

I am working on updating DB using Spring data JPA. I have update two tables simultaneously.
I was thinking about writing a query based on criteria and update but not sure how I would jot it down. Is there a better way to do it ? if so any tutorial link for that will be helpful.

Resources