java - Specifying list parameter in HQL NOT IN clause -
i've got following code set list
parameter in not in
clause in hql query
private static final string find_available_operators = "from domain domain domain.type = :type , 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 query } else { query = getcurrentsession().createquery(find_available_operators); query.setparameterlist("operators", domain.getoperators()); } query.setparameter("type", domaintype.operator); return query.list(); }
but sqlgrammarexception
when list
not empty
org.hibernate.exception.sqlgrammarexception: no value specified parameter 2
am using setparameterlist()
incorrectly?
the sql executed seems be
hibernate: select domain0_.domain_id domain1_2_, domain0_.country country2_, domain0_.name name2_, domain0_.type type2_ domain domain0_ cross join domain_operators operators1_, domain domain2_ domain0_.domain_id=operators1_.parent , operators1_.child=domain2_.domain_id , (. not in (?)) , domain0_.type=?
i've never seen (. not in (?))
.
edit: domain
entity
@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 }
as can see in the hql reference, [not] in
predicate works single value expressions.
you have use query such as:
private static final string find_available_operators = "select d domain d left outer join d.operators o d.type = :type , o.domainid not in (:operators)";
you can build list<long>
domain.getoperators()
ids , use in setparameterlist()
method.
Comments
Post a Comment