title: CriteriaBuilder in JPA - where clause
body: <p>Am new to JPA. Building the select query with the where clause. I need to select from table Contacts all ContactName's that equals the value of the String name.</p>
  
  <p>Used the code below to create DB table:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Following is my Entity class;</p>
  
  <pre><code>     @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;
      }
  </code></pre>
  
  <p>Following is my ContactServlet class code where i am trying to write my CriteriaBuilder code.</p>
  
  <pre><code>         EntityManager manager = null;
           EntityTransaction transaction = null;
           try{
  
          manager = this.factory.createEntityManager();
          transaction = manager.getTransaction();
          transaction.begin();
  
        CriteriaBuilder cb = manager.getCriteriaBuilder();
         CriteriaQuery&lt;Contact&gt; q1 = cb.createQuery(Contact.class);
         Root&lt;Contact&gt; postRoot = q1.from(Contact.class);
  
         q1.select(postRoot).where(cb.equal(postRoot.get("UserName"), name));
         TypedQuery&lt;Contact&gt; qry = manager.createQuery(q1);
  
         List&lt;Contact&gt; result = qry.getResultList();
          for (Contact contactInstance : result)  
          {
  
           DBContactName = contactInstance.getContactName().trim();
           DBContactEmail = contactInstance.getContactEmailID().trim();
  </code></pre>
  
  <p>.....</p>
  
  <p>Don't know where am i going wrong??</p>
  
  <p>Following is the error while executing my project:
  java.lang.IllegalArgumentException: Unable to resolve attribute [UserName] against path at org.hibernate.jpa.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:117) at org.hibernate.jpa.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:214) at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:185) at com.ContactServlet.doPost(ContactServlet.java:110) at javax.servlet.http.HttpServlet.service(HttpServlet.java:644) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) </p>