Apply Pagination on two different tables and merge results in spring boot - spring

I'm using Spring Data JPA to expose REST APIs. In my application, there are two types of tables available(current and archival) and structure of the current and archival tables are exactly similar and data will be moved for current table to archival table over the period of time for performance reasons. I'm having repository classes to retrieve the data from current and archival table separately and Pagination is also implemented for repositories.
Now I got a requirement to fetch the eligible records from both tables based on criteria and apply pagination at single shot. Is it possible with Spring Data JPA

You can keep the latest version in both tables and when you search for data you just do a regular search.
Another option would be to create a view over the two tables.
I also think Hibernate Envers was able to do that though I never tried it.

Related

Is it plausible to uswe spring-data-jdbc in a scenario where the DB-schema differs from the domain-model

I'm considering using spring-data-jdbc for a project.
But i don't have any control over the DB-schema.
My domain model can be populated by the existing tables, but they differ in many ways.
Examples:
A specific aggregate in my model consists of nested Value-Objects. The corresponding table only features flat columns, so the nested Value-Objects would have to be mapped manually.
One the other hand, there are aggregates that don't have many nested Value-Objects, but the corresponding tables are organized according to a star-schema, so the values are distributed over many tables (instead of a single one).
I guess this prevents me from using many of the Quality-Of-Life features (like Query-Derivation and Mapping).
Do I actually get anything significant out of spring-data-jdbc in comparison to using a plain JdbcTemplate in this scenario?
The scenario you describe would make me tend towards plain JdbcTemplate.
But I would consider using the Aggregate approach Spring Data JDBC does:
Load complete aggregates
Reference between aggregates using ids, or something like an AggregateReference
And if you have an aggregate that actually can be mapped using Spring Data JDBC you can still do that.

How to connect SpringBoot to multiple DataSource?

While there are resources available (on both SO and outside) that suggest how we can include multiple data sources in a SpringBoot application, all such resources assume that we have different entities on the different databases.
I have a use-case where I have two databases that I need to connect from the same SpringBoot application:
The first DB (say primary) is for Live data and
The other one is for historic data (say data older than 90 days).
The only difference between the data in these two DBs is the corresponding date (say InsertedDate) on which the record was inserted/updated.
Now as the two DBs have the exact same schema, I don't want to duplicate the corresponding entities and repositories in different packages just so that the two data source can scan different packages without any conflict.
If this is a SpringData JPA limitation, can someone suggest a better way to handle such scenario so that I can re-use my existing entity model?

Spring HATEOAS, how to handle conversion links to entities without flooding the DB

I'm using Spring boot 2.3, Spring Data REST, Spring HATEOAS, Hibernate.
Let's think to a simple use case like an user creating an invoice in a web client, or a inventory list for a warehouse. When the user submit the form, could be sent hundreds or rows and these rows can have links to other entities.
In the case of the invoice, for example, each row can have a product reference that will be passed to the sever as a link.
That link is translated by Spring into an entity using Repository. My point is that for every row, a query to get the product runs.
This means that everything will be really slow during insert (n+1 select problem).
Probably I missed somthing in the logic, but I didn't see concrete examples that focus on how to handle a big quantity of translations link -> entity.
Do you have any hint about it?
Is your point about many entities that will be created if linked entities will be returned to server? Hibernate (as well as spring) has lazy loading mechanism - https://blog.ippon.tech/boost-the-performance-of-your-spring-data-jpa-application/, so only necessary entities will be populated. Please, correct me if I miss understand your questions.

Old value in _aud table in envers

I have integrated hibernate envers in spring boot. Now my requirement is to have old value also for the particular column when value changed in *_AUD tables. However I cant see any feature available in Hibernate Envers plugin.
Please suggest.
Thanks
Unfortunately what you are looking to do just isn't someting supported.
It's one thing to think of an entity and needing to store basic type values such as strings or numeric data and have its old/new value represented by two columns in the audit table; however when you move beyond basic entity mappings to ones where you have relationships between entity types or collections; you begin to see that trying to store old/new data in the same row just isn't efficient and in some cases feasible.
That said, you can still read the audit history and deduce these old/new values using a variety of ways that include the Envers Query API, Debezium, or even basic database triggers.

Using Spring data jpa auditing to log into tables other than the entity’s table

In Hibernate Envers it is possible to have a separate audit table. Similarly, is it possible to log into tables other than the entity’s table using Spring Data JPA auditing?
The auditing feature of Spring Data JPA just fills attributes in the entity you are persisting. How and where these attributes get persisted is controlled by you JPA implementation and of course your database.
JPA offers #SecondaryTable to map fields to a second table.
If this isn't flexible enough for you, you can always employ database tools to achieve the effect by mapping the entity to a view which via triggers distributes the data however you want.

Resources