3

I need to filter an entity in a list of objects, for example:

public class Student {

    private int id;

    private List<Course> courses;

}

public class Course {

    private int id;

    private String name;

    private float note;

    private Classroom classroom;

}

public class Classroom {

    private int id;

    private String classroom;

} 

How to obtain a student object with a list of courses with only notes greater than 70, and located in classroom 23 (for example)?

Is there a way to use the name of the entity instead of the one of the column of the database?

Or how do I associate with sql the alias generated by hibernate for the entity?

I attach a link from the hibernate filters: https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch19.html

3
  • Have you tried any configuration yourself? Maybe you can post it and lets see whats wrong with it. Did you get any errors etc. Commented Jan 24, 2017 at 12:11
  • I just want to know if it can be filtered by the java entity, not by the database entity. Commented Jan 24, 2017 at 12:40
  • And how to do the join with the alias that generates hibernate dynamically. Commented Jan 24, 2017 at 12:43

1 Answer 1

5

Ok it think this should do the trick:

Entities

public class Student {

    private int id;

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

}

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

    private int id;

    private String name;

    private float note;

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

}


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

    private int id;

    private String classroom;

} 

Before query

Session session = sessionFactory.getCurrentSession();
session.enableFilter("defaultCoursesFilter");
session.enableFilter("defaultClassromFilter");

// query
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, I'll try.
This does not work for JPA queries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.