java - Formulating a JPA Criteria 'in' Expression -



java - Formulating a JPA Criteria 'in' Expression -

i requesting help in understanding how formulate 'in' status using javax.persistence.criteria package.

i creating criteriaquery based search criteria contacts class. contact can belong 0 many contact types. search criteria can include lastly name value, contact type or both.

when seek this:

expression<contacttype> param = criteriabuilder.parameter(contacttype.class); expression<list<contacttype>> contacttypes = fromcontact.get("contacttypes"); predicate newpredicate = param.in(this.getcontacttype(), contacttypes);

i get:

org.apache.openjpa.persistence.argumentexception: cannot execute query; declared parameters "parameterexpression<contacttype>" not given values. must supply value each of next parameters, in given order: [parameterexpression<contacttype>]

i haven't been able find illustration of how this. assitance , guidance apprecited. total code below.

class="lang-java prettyprint-override">public criteriaquery<contact> getsearchcriteriaquery(entitymanager entitymanager) { criteriabuilder criteriabuilder = entitymanager.getcriteriabuilder(); criteriaquery<contact> criteriaquery = criteriabuilder.createquery(contact.class); root<contact> fromcontact = criteriaquery.from(contact.class); predicate whereclause = criteriabuilder.equal(fromcontact.get("domain"), this.getdomain()); if (!stringutils.isempty(this.getlastname())) { predicate newpredicate = criteriabuilder.equal(fromcontact.get("lastname"), this.getlastname()); whereclause = criteriabuilder.and(whereclause, newpredicate); } if (this.getcontacttype() != null) { expression<contacttype> param = criteriabuilder.parameter(contacttype.class); expression<list<contacttype>> contacttypes = fromcontact.get("contacttypes"); predicate newpredicate = param.in(this.getcontacttype(), contacttypes); whereclause = criteriabuilder.and(whereclause, newpredicate); } homecoming criteriaquery.where(whereclause); } @entity @table(name = "contact") public class contact implements serializable { private static final long serialversionuid = -2139645102271977237l; private long id; private string firstname; private string lastname; private domain domain; private list<contacttype> contacttypes; public contact() { } @id @generatedvalue(strategy = generationtype.auto) @column(unique = true, nullable = false) public long getid() { homecoming this.id; } public void setid(long id) { this.id = id; } @column(name = "first_name", length = 20) public string getfirstname() { homecoming this.firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } @column(name = "last_name", length = 50) public string getlastname() { homecoming this.lastname; } public void setlastname(string lastname) { this.lastname = lastname; } //bi-directional many-to-one association domain @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "domain") public domain getdomain() { homecoming this.domain; } public void setdomain(domain domain) { this.domain = domain; } @manytomany @jointable(name = "contact_cnttype", joincolumns = { @joincolumn(name = "contact", referencedcolumnname = "id")}, inversejoincolumns = { @joincolumn(name = "contact_type", referencedcolumnname = "id")}) public list<contacttype> getcontacttypes() { homecoming this.contacttypes; } public void setcontacttypes(list<contacttype> contacttypes) { this.contacttypes = contacttypes; } }

you have set parameter value query when list results:

typedquery<entity> q = this.entitymanager.createquery(criteriaquery); q.setparameter(contacttype.class, yourcontacttypevaluetofilter); q.getresultlist();

what

criteriabuilder.parameter(contacttype.class);

does, create parameter in query, need bind later.

java jpa-2.0 criteria-api

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -