Skip to content

"Unknown Column" Error on Inherited Field with Spring Data PageRequest.of() #3980

@onbao165

Description

@onbao165

I'm encountering an error Unknown column 'createdAt' in 'order clause' error when passing "createdAt" property with Spring Data's PageRequest.
The createdAt field is inherited from a @MappedSuperclass. I'm correctly using the entity property name, not the column name. The strange part is that the error often goes away after a few application restarts, only to reappear later.
Why does Spring Data JPA/Hibernate intermittently fail to resolve the correct column name for an inherited property during sorting, and what is the correct way to prevent this from happening?

Problematic Code:

Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize(), Sort.by("createdAt").ascending());
Page<Submission> page = submissionRepo.findAll(pageable);

The Error:

[http-nio-8080-exec-2] DEBUG org.hibernate.SQL - SELECT * FROM submissions WHERE user_id = ? order by createdAt asc limit ?
[http-nio-8080-exec-2] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1054, SQLState: 42S22
[http-nio-8080-exec-2] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Unknown column 'createdAt' in 'order clause'

Entity class and its Parent

@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "app_users")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
// Note that @SQLRestrictions are always applied and cannot be disabled. Nor may they be parameterized. They're therefore much less flexible than filters.
@SQLRestriction("is_deleted = false")
public class AppUser extends BaseAuditableEntity<UUID> {
  ....
}

@MappedSuperclass
@Data
@EqualsAndHashCode(callSuper = true)
@EntityListeners(AuditingEntityListener.class)
public class BaseAuditableEntity<K> extends BaseEntity<K> implements Auditable, SoftDeletable {
    @CreatedDate
    @Column(name = "created_at", updatable = false, nullable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @Column(name = "is_deleted")
    private boolean isDeleted;

    @Column(name = "deleted_at")
    private LocalDateTime deletedAt;
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions