我的对象:用户和凭据- 多对多关系,但是用户有一个参数
我想为循环中的每个凭据获取具有特定参数的所有用户
要求:用户必须批量加载。
- 简单吧?
所以我有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 查询,该查询获取所有用户的凭据,而不是所有具有指定参数的用户的凭据(换句话说,未应用过滤器)
任何建议如何使它工作?
谢谢 !