spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
2.92k stars 1.39k forks source link

Implement nested Hibernate filters for entities #3524

Closed IlyaLisov closed 2 days ago

IlyaLisov commented 3 days ago

I suggest to implement nested filtering by using @Filter annotation on @Entity while using JPA repositories.

Like example:

public class Student {

    private int id;

    @OneToMany(mappedBy = "student")
    @Filter(name = "defaultCoursesFilter")   
    private List<Course> courses;

}

@FilterDef(name = "defaultCoursesFilter"
                , condition=" notes > 70")
public class Course {

    private int id;

    private String name;

    private float note;

    @ManyToOne
    @Filter(name = "defaultClassromFilter")
    private Classroom classroom;

}

@FilterDef(name = "defaultClassromFilter"
                , condition=" id  = 23")
public class Classroom {

    private int id;

    private String classroom;

} 

For now as I tried with different entities it does not work, so I need to duplicate filters or write custom SQL queries (or other different methods)

mp911de commented 3 days ago

Filters are a Hibernate-specific feature that needs to be enabled through Hibernate's Session. There isn't much we can do here.

IlyaLisov commented 3 days ago

@mp911de thank you for answer