Elasticsearch: StackoverflowError when trying to perform re-indexing based on JPA entities with relationships
See original GitHub issueOverview of the issue
The normal indexing mechanism of elastic search repos works while saving an entity into database. When trying to implement a custom reindexing mechanism (as my data is not coming in through some frontend at the moment 😕), I ran into an issue with taking entities directly from database (JPA entities) and saving them within the elastic search repository. Something like:
searchRepo.saveAll(entityRepo.findAll())
This lead to a StackoverflowError for entities with relationships as Elastic didn’t understand to break the infinite cycle that we have due to the bidirectionality in the generated entities’ relationship fields.
I tried various things and finally had some sort of success, but I still like to have this noted down somewhere to investigate further if there is a better solution or if it even makes sense to integrate something into generator code to just be able to support this kind of use case e.g. in a new blueprint).
Added
@Field(type = FieldType.Nested, ignoreFields = { "subField" })
on – still produced the error
Added
(fetch = FetchType.LAZY)
– still produced the error
Added
@Transient
worked, but cleared the relationships also in database, as I have put all my re-indexing code into a @Transactional
annotated class. (Seems to be cause by using the wrong @Transient
😐 - the Spring data one is the correct one, the javax one will cause the mentioned issues)
Fetch the data from database -> map to dto -> map back to entity -> save to search repository – works, but looks ugly.
I also just found this old piece of code https://github.com/geraldhumphries/generator-jhipster-elasticsearch-reindexer/blob/e300378cfc19c9a747b9aa2a43836f3807e369d2/generators/app/templates/src/main/java/package/service/_ElasticsearchIndexService.java#L186-L202 –> The relationships are loaded manually. Will check if that is somehow also an option…
Motivation for or Use Case
Implementation of re-indexing mechanisms would be nice to be able to do without big hassle.
Reproduce the error
TBD
Related issues
#16136 --> #18239 According to some comment in #16136 this issue can be avoided when directly initializing the index properly with all relationships known… will check that out. --> Doesn’t work. I have put the Nested type + ignoreFields everywhere and started with a fresh index, still failing.
TBD
Suggest a Fix
TBD
JHipster Version(s)
7.8.1
JHipster configuration
TBD
Entity configuration(s) entityName.json
files generated in the .jhipster
directory
TBD
Browsers and Operating System
- Checking this box is mandatory (this is just to show you read everything)
Issue Analytics
- State:
- Created a year ago
- Comments:11 (10 by maintainers)
As discussed in #18741 possible solution could be to place @Transient annotation on all generated relations. This will prevent circular dependencies, but also querying over the object boundaries.
Change will be pretty trivial though.
Some thoughts: JDL allows to place annotations on the properties, but not on the relationships. Allowing this will provide clean solution which could have some other use cases as well.
In the current situation users will receive stack overflows out of sudden when some object is changed, and circular dependency between entities emerges. And there is also potential to slurp entire database in one transaction by following relations between objects ( hibernate will do lazy loading , but elasticsearch will follow all the relationships unless prevented ) This extremely annoying to developers (and has potential to backfire) and lowers the acceptance of jHipster stack by architects / decision makers
See: https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping
Putting @Transient by default will be easiest solution and provide reasonable default. and adding some annotation / option to relationship will allow to customise it for advanced users who know what they are doing ( There are already @JsonIgnore annotations generated on relations, which are of no use with new elasticsearch )
I think it was already taken care of at least in the development version.
========[] Lines game back in Appstore - check it out! []=============== https://play.google.com/store/apps/details?id=de.pribluda.games.android.lines
Blog: https://www.pribluda.de
On Thu, 18 Aug 2022 at 22:22, Rizzi @.***> wrote: