简体   繁体   English

如何使用休眠过滤器在休眠中过滤实体

[英]How to filter an entity in hibernate with hibernate filters

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)? 如何获得一个学生的对象,该对象的课程清单(例如)位于教室23中,且其笔记仅大于70。

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? 或者我如何与sql关联由hibernate为实体创建的别名?

I attach a link from the hibernate filters: https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch19.html 我附上了来自休眠过滤器的链接: https : //docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch19.html

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM