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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18196210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:12:27  来源:igfitidea点击:

Specifying list parameter in HQL NOT IN clause

javahibernatehql

提问by Sotirios Delimanolis

I've got the following code to set a Listparameter in a NOT INclause in an HQL query

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

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

but I get an SQLGrammarExceptionwhen my Listis not empty

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

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

Am I using the setParameterList()incorrectly?

我使用setParameterList()错误吗?

The SQL executed seems to be

执行的 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=?

I've never seen (. not in (?)).

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

EDIT: My Domainentity

编辑:我的Domain实体

@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
    }

采纳答案by Marcelo

As you can see in the HQL reference, the [not] inpredicate works only with single value expressions.

正如您在HQL 参考中所见,[not] in谓词仅适用于单值表达式。

You will have to use a query such as:

您将不得不使用查询,例如:

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

You then can build a List<Long>with just the domain.getOperators()IDs and use it in the setParameterList()method.

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