question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Elasticsearch: StackoverflowError when trying to perform re-indexing based on JPA entities with relationships

See original GitHub issue
Overview 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:closed
  • Created a year ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ko5tikcommented, May 28, 2022

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 )

0reactions
ko5tikcommented, Aug 19, 2022

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:

Hey…

I have so StackoverflowError with v7.9.2 on OneToOne relations. If I put @transient https://github.com/transient to both sides, not only on one, it work’s for me. I don’t know if it is also on previous versions.

— Reply to this email directly, view it on GitHub https://github.com/jhipster/generator-jhipster/issues/18729#issuecomment-1219922591, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABSUDWWPRB635AQCXADGA3VZ2LPNANCNFSM5W2KTZGA . You are receiving this because you were mentioned.Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Indexing problem with Spring Data Elastic migration from 3.x ...
Now with Spring data elastic, the Jackson based mapper is not available we are seeing multiple StackOverflow errors. Below is the migration ...
Read more >
OneToOne bidirectional StackOverflow error - Google Groups
Hi, There seems to be a bug with OneToOne bidirectional relationship. For example take entities: @Entity @Table(name="wheel") public class Wheel {
Read more >
spring-projects/spring-data - Gitter
Hi, I am trying to use persistent Entities that are composed using traits in groovy but having some ... There is spring-data-jpa 2.0.9...
Read more >
Hibernate Search in Quarkus - In Relation To
Let's take this Quarkus release as an opportunity to have a closer ... API to seamlessly search for entities based on Elasticsearch indexes....
Read more >
Hibernate Search 6.1.7.Final: Reference Documentation
If you do not want to, or cannot, fetch the JARs from a Maven ... search generally use an entity-based model to represent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found