[英]filter lazy initialized collection
我的對象: 用戶和憑證 - 多對多關系,但用戶有一個參數
我想讓所有用戶在循環中獲得每個憑據的特定參數
要求:必須批量加載用戶。
所以我有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查詢,該查詢獲取憑證的所有用戶,而不是所有具有指定的憑證參數的用戶(換句話說,未應用過濾器)
有人建議如何使它工作?
謝謝 !
我通過將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得到它......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.