3

我的对象:用户凭据- 多对多关系,但是用户有一个参数

我想为循环中的每个凭据获取具有特定参数的所有用户

要求:用户必须批量加载。

  • 简单吧?

所以我有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
}

我在一个地方进行查询并返回结果:

    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;

同时对每个凭证进行一些处理,现在我需要获取具有给定参数的用户列表:

    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);
    }

我的问题是迭代器生成 SQL 查询,该查询获取所有用户的凭据,而不是所有具有指定参数的用户的凭据(换句话说,未应用过滤器)

任何建议如何使它工作?

谢谢 !

4

1 回答 1

0

我通过将 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;

我无法通过 UserCredentialMapping 让它工作......

于 2012-09-22T19:12:45.173 回答