我的对象:用户和凭据 - 多对多关系,但用户有一个参数
我希望所有用户都能获得循环中每个凭据的某个参数
要求:必须批量加载用户。
所以我有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查询,该查询获取所有用户的凭证,而不是所有用户指定的参数用于凭证(换句话说,过滤器未被应用)
有任何建议如何让它发挥作用?
谢谢!
答案 0 :(得分:0)
我通过向Credential类添加Many To Many映射来解决它:
@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;
我无法通过UserCredential Mapping ...
来完成它