java - CriteriaBuilder in JPA - where clause -
am new jpa. building select query clause. need select table contacts contactname's equals value of string name.
used code below create db table:
create table contacts ( contactid bigint unsigned not null primary key auto_increment, contactname varchar(100) not null, contactemailid varchar(100) not null, username varchar(100) not null, index contact_names (contactname) ) engine = innodb;
following entity class;
@entity private string username; @id @generatedvalue(strategy = generationtype.identity) @column(name = "contactid") public long getcontactid() { return contactid; } public void setcontactid(long contactid) { contactid = contactid; } @basic @column(name = "username") public string getusername() { return username; } public void setusername(string username) { this.username = username; }
following contactservlet class code trying write criteriabuilder code.
entitymanager manager = null; entitytransaction transaction = null; try{ manager = this.factory.createentitymanager(); transaction = manager.gettransaction(); transaction.begin(); criteriabuilder cb = manager.getcriteriabuilder(); criteriaquery<contact> q1 = cb.createquery(contact.class); root<contact> postroot = q1.from(contact.class); q1.select(postroot).where(cb.equal(postroot.get("username"), name)); typedquery<contact> qry = manager.createquery(q1); list<contact> result = qry.getresultlist(); (contact contactinstance : result) { dbcontactname = contactinstance.getcontactname().trim(); dbcontactemail = contactinstance.getcontactemailid().trim();
.....
don't know going wrong??
following error while executing project: java.lang.illegalargumentexception: unable resolve attribute [username] against path @ org.hibernate.jpa.criteria.path.abstractpathimpl.unknownattribute(abstractpathimpl.java:117) @ org.hibernate.jpa.criteria.path.abstractpathimpl.locateattribute(abstractpathimpl.java:214) @ org.hibernate.jpa.criteria.path.abstractpathimpl.get(abstractpathimpl.java:185) @ com.contactservlet.dopost(contactservlet.java:110) @ javax.servlet.http.httpservlet.service(httpservlet.java:644) @ javax.servlet.http.httpservlet.service(httpservlet.java:725) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:291) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:505) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:610) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:534) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1081) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:658) @ org.apache.coyote.http11.http11nioprotocol$http11connectionhandler.process(http11nioprotocol.java:222) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1566) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1523) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745)
try with:
q1.select(postroot).where(cb.equal(postroot.get("username"), name));
the u of username attribute in lower case.
Comments
Post a Comment