Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different property is used in Sort.Order method #3477

Closed
IlyaLisov opened this issue May 15, 2024 · 2 comments
Closed

Different property is used in Sort.Order method #3477

IlyaLisov opened this issue May 15, 2024 · 2 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@IlyaLisov
Copy link

I have two entities

@Entity
@Table(name = "articles")
public final class Article {
    ...
    private int viewsAmount;

    public enum SortType {
        ...,
        VIEWS_AMOUNT
    }
}
@Entity
@Table(name = "employees")
public final class Employee {
    ...
    private int viewsAmount;

    public enum SortType {
        ...,
        VIEWS_AMOUNT
    }
}

Next, I want to sort this entities by using Sort.Order object:

public Pageable getPageable() {
    Sort.Order order = Sort.Order.desc("views_amount");
    return PageRequest.of(
            this.pageId - 1,
            this.perPage,
            Sort.by(order)
    );
}

When I call repository method with custom @Query, I get it working. Entities are loaded.

@Query(value = """
        SELECT em.*
        FROM employees em
        WHERE em.status = :status
        AND em.type LIKE coalesce(:type, '%%')
        """, nativeQuery = true)
Page<Employee> findAllByStatusAndType(
        @Param("status") String status,
        @Param("type") String type,
        Pageable page
);

When I call repository method without custom @Query, it is not working with exception No property 'views' found for type 'Article'. I don't even understand why it is views here in the exception.

Page<Article> findAllByStatus(
        Status status,
        Pageable page
);

It works if I change order object to Sort.Order.desc("viewsAmount") as it is names in @Entity.

In the documentation I did not find what does property mean in Sort.Order methods. I thought it means database column names, but as I see here, it can also mean @Entity field.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 15, 2024
@quaff
Copy link
Contributor

quaff commented May 16, 2024

Could you provide a minimal sample project?

@mp911de
Copy link
Member

mp911de commented May 16, 2024

This is correct and expected. Sort.Order.desc("views_amount"); would refer to a nested property amount in the views property of Article or Employee. Sort refers always to property names unless the query is a native one. Spring Data uses _ as separator to force property name splitting.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
@mp911de mp911de added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants