【问题标题】:Hibernate Example Criteria query: filtering by child propertiesHibernate 示例条件查询:按子属性过滤
【发布时间】:2012-02-17 18:41:10
【问题描述】:

我做了相当多的研究,最后决定问这个。

我有两个这样的课程:

Employee
-emp_id
-name
-dep_id

Department
-dep_id
-name

我用这段代码举例查询:

List<Employee> find = null;
Example example = Example.create(criteria)
        .excludeZeroes()
        .ignoreCase();
find = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Employee.class)
        .add(example)
        .list() ;
return find;

条件对象是 Employee 的一个实例,我想检索具有给定部门名称的所有员工。

问题是,当我执行代码时,即使标准具有这样的属性集,我也会从所有部门获得员工: criteria.department.name = "IT"

当示例设置了父属性但不过滤子属性时,它可以正常工作。

据我所知,我必须创建别名才能加入子属性,但这违背了示例标准的目的。

有这方面的cmet吗?

【问题讨论】:

    标签: hibernate criteria query-by-example


    【解决方案1】:

    使用createCriteria(Department.class) 创建的条件不会返回员工。它将返回部门。

    示例查询用于检索与您作为参数传递的示例相同的实体。但是,正如documentation 所说:

    版本属性、标识符和关联被忽略

    【讨论】:

    • 你是对的,写问题的时候出错了。我已对其进行了更新并将其标记为已回答。我将修改代码以添加条件以按子实体进行过滤。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2013-06-07
    • 2016-05-22
    • 2013-10-01
    • 2017-03-24
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多