gpt4 book ai didi

java - 在 HQL NOT IN 子句中指定列表参数

转载 作者:搜寻专家 更新时间:2023-11-01 01:10:31 24 4
gpt4 key购买 nike

我有以下代码在 HQL 查询的 NOT IN 子句中设置 List 参数

private static final String FIND_AVAILABLE_OPERATORS = "FROM Domain domain WHERE domain.type = :type AND domain.operators NOT IN (:operators)";

@SuppressWarnings("unchecked")
@Override
public List<Domain> findAvailableOperators(Domain domain) {
Query query = null;
if (domain.getOperators().isEmpty()) {
query = getCurrentSession().createQuery(FIND_BY_DOMAIN_TYPE); // run another query
} else {
query = getCurrentSession().createQuery(FIND_AVAILABLE_OPERATORS);
query.setParameterList("operators", domain.getOperators());
}

query.setParameter("type", DomainType.OPERATOR);
return query.list();
}

但是当我的 List 不为空时,我得到一个 SQLGrammarException

org.hibernate.exception.SQLGrammarException: No value specified for parameter 2

我使用的是 setParameterList() incorrectly

执行的SQL好像是

Hibernate: select domain0_.domain_id as domain1_2_, domain0_.country as country2_, domain0_.name as name2_, domain0_.type as type2_ from domain domain0_ cross join domain_operators operators1_, domain domain2_ where domain0_.domain_id=operators1_.parent and operators1_.child=domain2_.domain_id and (. not in  (?)) and domain0_.type=?

我从未见过 (. not in (?))

编辑:我的实体

@Entity
@Table
public class Domain {
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "domain_id")
private Long domainId;

@Column(nullable = false, unique = true)
@NotNull
private String name;

@Column(nullable = false)
@NotNull
@Enumerated(EnumType.STRING)
private DomainType type;

@ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
}, fetch = FetchType.EAGER)
@JoinTable(joinColumns = {
@JoinColumn(name = "domain_id")
}, inverseJoinColumns = {
@JoinColumn(name = "code")
})
private Set<NetworkCode> networkCodes = new HashSet<>();

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(joinColumns = {
@JoinColumn(name = "parent", referencedColumnName = "domain_id")
}, inverseJoinColumns = {
@JoinColumn(name = "child", referencedColumnName = "domain_id")
})
private Set<Domain> operators = new HashSet<>();

@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Country country;

public Domain() {}
... setters/getters
}

最佳答案

正如您在 the HQL reference 中看到的那样, [not] in谓词仅适用于单值表达式。

您将不得不使用如下查询:

private static final String FIND_AVAILABLE_OPERATORS = "SELECT d FROM Domain as d LEFT OUTER JOIN d.operators as o WHERE d.type = :type AND o.domainID not in (:operators)";

然后您可以构建一个 List<Long>只有 domain.getOperators() ID 并在 setParameterList() 中使用它方法。

关于java - 在 HQL NOT IN 子句中指定列表参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18196210/

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