gpt4 book ai didi

java - 过滤延迟初始化集合

转载 作者:行者123 更新时间:2023-12-02 23:24:48 25 4
gpt4 key购买 nike

我的对象:用户凭据 - 多对多关系,但用户有一个参数

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

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

  • 很简单吧?

所以我有 3 张 table :

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

有什么建议如何让它发挥作用吗?

谢谢!

最佳答案

我通过向 Credential 类添加 ManyToMany 映射解决了这个问题:

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

关于java - 过滤延迟初始化集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12535900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com