Skip to content

Spring Data JDBC - Sort.by does not work on @Embedded property #1286

@pallocchi

Description

@pallocchi

Hi guys!
I'm trying to apply sorting in a Pageable, but there is no way to make it work for a property which is part of an @Embedded object.

Entities:

data class Account(
    @Id
    val id: Long?,
    @Embedded.Nullable(prefix = "contact_")
    val contact: Contact?,
)
data class Contact(
    val name: String,
)

Sort:

Sort.by("contact.name").ascending()

Exception:

java.lang.IllegalStateException: Required property contact.name not found for class lms.model.Account!
	at org.springframework.data.mapping.PersistentEntity.getRequiredPersistentProperty(PersistentEntity.java:161) ~[spring-data-commons-2.6.0.jar:2.6.0]

If the PersistentEntity.getRequiredPersistentProperty method doesn't know anything about the @Embedded annotation, I think the following method won't work and it could be the cause of the issue.

private OrderByField orderToOrderByField(Sort.Order order) {
SqlIdentifier columnName = this.entity.getRequiredPersistentProperty(order.getProperty()).getColumnName();
Column column = Column.create(columnName, this.getTable());
return OrderByField.from(column, order.getDirection()).withNullHandling(order.getNullHandling());
}

Should you use the SqlGenerator.getTable().column() method instead?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions