2012-09-21 135 views
3

我的對象:用戶憑證 - 多對多的關係,但是用戶設置了一個param濾波延遲初始化集合

我想某些PARAM所有用戶對每個證書在一個循環

要求:用戶必須批量加載。

  • 簡單吧?

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

在平均時間每個憑據一些處理,現在我需要得到用戶的名單給定PARAM:

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

我的親blem是迭代產生(沒有施加換句話說過濾器)的SQL查詢得到的證書,所有用戶並不是所有的用戶與指定PARAM憑據

任何建議如何使它工作?

謝謝!

回答

0

我已通過添加多對多映射到憑證類解決了這個問題:

@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工作...