0

I am working on integrating spanner with our existing application. I'm facing an error when trying to save/persist data into DemoClass entity in spanner using JPA and hibernate 6. The error I'm facing is as under

Error: Number of joins exceeds the maximum allowed limit of 20.

Entity class

@Entity
DemoClass extends CommonClass{
...
...

//getter and setter
}

@MappedSuperClass
CommonClass implements Serializable {

@CreationTimeStamp
Timestamp CreatedTime;

@CreationTimeStamp
Timestamp updatedTime;

@OneToOne
Demo2 createdUser;

@OneToOne
Demo2 updatedUser;

//getter and setter
}

@Entity
Demo2 extends CommonClass {
...
...

//getter and setter
}

Dependencies from pom.xml

spring-boot-starter-parent: 3.3.5
spring-boot-starter-data-jpa
google-cloud-spanner-hibernate-dialect: 3.7.1
google-cloud-spanner: 6.81.2
spring-cloud-gcp-data-spanner: 5.8.0
google-cloud-spanner-jdbc : 2.24.1
hibernate-types-60: 2.21.1

2
  • What is the question exactly? Spanner lists this limitation here: cloud.google.com/spanner/quotas
    – Chris
    Commented Dec 4, 2024 at 21:17
  • Demo2 has two references to Demo2; That is going to confuse things. Maybe make your 'user' class not self referencing, or have lazy relationships in there so they aren't automatically joined
    – Chris
    Commented Dec 4, 2024 at 21:20

1 Answer 1

0

The error you're encountering, "Number of joins exceeds the maximum allowed limit of 20," typically happens when a query involves too many table joins, which could be related to how your entity relationships are set up, especially with the @OneToOne associations in your CommonClass and Demo2 entities.

Here are a few things to try:

  • Check Lazy Loading: By default, @OneToOne relationships are eagerly loaded, which could lead to multiple joins being triggered when persisting or querying the data. Try annotating the relationships as @OneToOne(fetch = FetchType.LAZY) to avoid loading the related entities unnecessarily during the persistence operation.
@OneToOne(fetch = FetchType.LAZY)
Demo2 createdUser;

@OneToOne(fetch = FetchType.LAZY)
Demo2 updatedUser;
  • Review the Entity Structure: Since you're extending CommonClass, which itself has the @OneToOne relationships, make sure these relationships aren't leading to unnecessary or excessive joins. In some cases, an @MappedSuperclass might be included multiple times in the query, resulting in many joins. You could also consider restructuring your model if the relationship depth is too complex.

  • Optimize Queries: Check the generated SQL queries (via logging) and see if there are excessive or unintended joins. You might be able to optimize them or simplify the persistence logic. You can enable Hibernate SQL logging by adding this to your application.properties:

spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.generate_statistics=true

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.