【问题标题】:filter lazy initialized collection过滤惰性初始化集合
【发布时间】:2012-09-14 04:38:18
【问题描述】:

我的对象:user 和 credential - 多对多关系,但是用户有参数

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

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

  • 简单吧?

所以我有 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 查询,该查询获取所有用户的凭据,而不是所有用户具有指定参数的凭据(换句话说,未应用过滤器)

任何建议如何使它工作?

谢谢!

【问题讨论】:

    标签: java hibernate filter lazy-loading


    【解决方案1】:

    我通过将 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 让它工作...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 2011-07-21
      • 2017-03-11
      相关资源
      最近更新 更多