本文介绍了如何使用休眠过滤器在休眠中过滤实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要过滤对象列表中的实体,例如:

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;

}

例如,如何获取位于教室23中的,仅具有大于70的笔记的课程列表的学生对象?

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?

或者我该如何与sql关联由hibernate为实体创建的别名?

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

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

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

推荐答案

好吧,它认为应该可以解决问题:

Ok it think this should do the trick:

实体

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;

}

查询之前

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

// query

这篇关于如何使用休眠过滤器在休眠中过滤实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:24
查看更多