简体   繁体   English

过滤延迟初始化集合

[英]filter lazy initialized collection

My objects: user and credential - many to many relationship, however user has a param 我的对象: 用户凭证 - 多对多关系,但用户有一个参数

I want to get all users with certain param for every credential in a loop 我想让所有用户在循环中获得每个凭据的特定参数

requirement: users have to be loaded in batch. 要求:必须批量加载用户。

  • simple right? 简单吧?

so i have 3 tables: 所以我有3个表:

@Table(name = "CRED")
public class Credential {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name="CRED_ID")     
    Long credentialId;

    @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "credential")
    @BatchSize(size = Search.DEFAULT_PAGE_SIZE)
    private Set<UserCredentialMapping> userCredentialMappingSet;  
}

@Table(name = "USER_CRED")
public class UserCredentialMapping {

    @JoinColumn(name = "user_id", referencedColumnName = "user_id")
    @ManyToOne
    @Filter(name="paramFilter", condition="param = :param")
    private User user;

    @JoinColumn(name = "cred_id", referencedColumnName = "cred_id")
    @ManyToOne
    private Credential credential;
}

@Table(name = "USER")
public class User  {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name="USER_ID")     
    Long userId;

    @Column(name = "PARAM")
    String param
}

i'm making a query in one place and return results: 我在一个地方进行查询并返回结果:

    String hqlQuery =   "select c from UserCredentialMapping m " +
        " inner join m.credential c" +
        " inner join m.user u" +
        " where u.param = :param" +
        " and c.user_id in (:users)" ;

        Session session = getSession();
        //desparetly trying to set filter
        session.enableFilter("paramFilter").setParameter("param", param);

    Query query = session.createQuery(hqlQuery);
    query.setParameterList("users", USERLIST);
    query.setParameter("param", someparam);

    List<Credential> credentialList = (List<Credential>)query.list();
    return credentialList;

some processing on each credential in mean time and now i need to get list of users with given param: 平均时间对每个凭证进行一些处理,现在我需要获得给定参数的用户列表:

    for(Credential credential : credentialList){

        //following line makes hibernate query for users
        Iterator<CredentialMapping> mappingIterator = e.getUserCredentialMappingSet().iterator();

        while (mappingIterator.hasNext()){
            UserCredentialMapping userCred = mappingIterator.next();

            User user = userCred.getUser(); 
            DOEVILSTUFFTOINNOCENT(user);
    }

My problem is that iterator generates SQL query that gets all users for credential and not all users with specified param for credential (in other words filter is not being applied) 我的问题是迭代器生成SQL查询,该查询获取凭证的所有用户,而不是所有具有指定的凭证参数的用户(换句话说,未应用过滤器)

Any advise how to make it work? 有人建议如何使它工作?

Thanks ! 谢谢 !

I've solved it by adding ManyToMany mapping to Credential class: 我通过将ManyToMany映射添加到Credential类来解决它:

@ManyToMany(fetch = FetchType.LAZY)
@NotFound(action = NotFoundAction.IGNORE)
@JoinTable(name = "USER_CRED", 
    joinColumns = { 
        @JoinColumn(name = "CRED_ID") }, 
    inverseJoinColumns = { 
        @JoinColumn(name = "USER_ID") })
@Filter(name="param", condition="PARAM = :param")
@BatchSize(size = Search.DEFAULT_PAGE_SIZE)
private Set<User> users;

i couldn't get it working through UserCredentialMapping... 我无法通过UserCredentialMapping得到它......

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

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