I am implementing a relationship between two entities in a Java Spring application for an e-commerce site, using PostgreSQL as a database. The entities involved are:
User (with an identifier id_user).
ArticoloPolicySconto (with a composite key: id_articolo and politica_sconto_id).
The relationship between these entities is defined as @ManyToMany. Specifically:
The User entity has a field idPoliticaSconto, which corresponds to articolo_politica_sconto.idPoliticaScontoVers.
Each idPoliticaScontoVers in the articolo_politica_sconto table can be associated with multiple articles.
Problem:
When I establish the association between a user and the corresponding ArticoliPoliticheSconto, since every time during the loading another relation between the PoliticaSconto table and the Articoli table is called into play (7000 and some records), the data involved amounts in total to about 35,000,000 records, causing a significant impact on performance (infinite loading of data, estimated 1 week) if the relation is loaded or processed automatically, for example, at application startup.
Question:
Is it possible to configure the association so that the data is loaded only when a specific method is called (for example, based on the user_id), avoiding loading the entire relation during the application startup phase? The goal is to optimize queries and reduce timing, avoiding managing all the data at the same time.
In other words, the request is to know if it is possible to configure the relation for lazy loading or a similar strategy, in order to reduce the data management overhead during initialization.
I have tried the calls in batch mode and also in fetch type eager mode. I expect a fast way to load only the data that the logged in user needs, specifically his prices, and not the whole database.
Luca is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1