简体   繁体   English

多个限制的休眠独立条件

[英]Hibernate detached criteria for multiple Restrictions

I am trying to fetch list of Users whose status still shows as inactive and process them. 我正在尝试获取状态仍显示为非活动的用户列表并进行处理。 This User is embedded with UserDetails, UserActivity entities. 该用户嵌入了UserDetails,UserActivity实体。 All entity tables have status column with value as Inactive. 所有实体表的状态列的值为“无效”。

Right now I am using detached criteria as, 现在,我使用独立标准,因为

detachedcriteria.for class(User.class).add(Restrictions. 
   Eq("status","Inactive").setresulttransformer(criteria.distinct_element)

This is giving me User object along with its embedded entities as a single User object. 这给了我用户对象及其嵌入的实体作为单个用户对象。

But now, I have to tune the query to make sure status is inactive for embedded class as well. 但是现在,我必须调整查询以确保嵌入式类的状态也处于非活动状态。 So I did opted to go for named query as I don't know how to add multiple Restrictions for different entities in a single detached criteria. 因此,我选择了命名查询,因为我不知道如何在单个分离条件中为不同实体添加多个限制。

In named query I am joining all tables to fetch the data. 在命名查询中,我将联接所有表以获取数据。 And I was able to get the data but its returning list of object array. 而且我能够获取数据,但返回对象数组的列表。 Where in first object of this list contain array of below entities. 此列表的第一个对象中包含以下实体的数组。

Named query: 命名查询:

from User us, UserDetail ud, UserActivity uc where us.id=ud.id and us.id=uc 
  .id and us.status=ud.status and us.status=uc.status and us.status='Inactive'

List<User>=query.list ();

Object ob=lst.get(0);

Where ob contains [User(),UserDetail(),UserActivity ()].
  1. Can we right multiple Restrictions for different entities in a single detached criteria? 我们可以在一个单独的条件中对不同实体使用多个限制吗?

  2. Can we have a named query which will return me a single User object along with embedded entities in it as I am getting in detachedcriteria?(So that I can directly perform User.getUserDetail() ). 我们可以有一个命名查询,当我进入分离标准时,该查询将返回我一个用户对象以及其中的嵌入实体吗(这样我就可以直接执行User.getUserDetail() )。

You can use aliases for this: 您可以为此使用别名:

Assuming you have User class like 假设您有User类,例如

class User {
    ...
    private UserDetail userDetail;
    private UserActivity userActivity;
    ...
}

You can not directly put restrictions to second level properties of your object. 您不能直接对对象的第二级属性设置限制。 By using .createAlias() method you can add JOIN statements to your generated SQL and add restrictions by using aliases. 通过使用.createAlias()方法,可以将JOIN语句添加到生成的SQL中,并使用别名添加限制。

DetachedCriteria dc = DetachedCriteria
    .forClass(User.class , "u")
    .add(Restrictions.Eq("u.status","Inactive")
    .createAlias("u.userDetail", "ud")
    .add(Restrictions.Eq("ud.status","Inactive")
    .createAlias("u.userActivity", "ua")
    .add(Restrictions.Eq("ua.status","Inactive")
    .setResultTransformer(Criteria.DISTINCT_ROOT_ELEMENT);

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

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