Search code examples
spring-data-jdbc

Spring Data JDBC column alias in derived query getting truncated


I am using Spring Data JDBC 2.0.5 (as pulled in by Spring Boot 2.3.5) on top of Postgres 11.10.

In our domain model we have an aggregate root that looks something like the following:

@Table(...)
class OurAggregateRoot {
  
  @MappedColumn(...) // 1:1 relationship
  private final ReallyLongClassNameForThisEntity reallyLongClassNameForThisEntity;
}
@Table(...)
class ReallyLongClassNameForThisEntity {
  
  @MappedColumn(really_long_class_name_id) // 1:1 relationship
  final AnotherReallyLongClassName anotherReallyLongClassName;
}

Given that we have three tiers of relationships and long class names we've run into a situation where a column alias in the query that was generated when calling CrudRepository::findById exceeded the character limit as set by PostgreSQL. E.g. the column alias for the identifier AnotherReallyLongClassName uses to reference its parent entity would be reallyLongClassNameForThisEntity_anotherReallyLongClassName_really_long_class_name_id.

Is there something we can do to safeguard against this aside from renaming class/field/column names and limiting the number of nested relationships within an aggregate root? Trying to change the character limit on Postgres alias names doesn't appear to be an easy option for us.


Solution

  • There is no direct way to avoid this. Things that help are: using shorter names for columns as you said and taking a hard look at your aggregates: Is it really appropriate to have such deeply nested aggregates?