title: How to select multiple records from table in hibernate
body: <p>I am trying to get multiple values from a web page form and search the value from databate using hibernate query and retrieve the result and display it in a new page. </p>
  
  <p><img src="https://i.stack.imgur.com/mBqg7.png" alt="enter image description here"></p>
  
  <p>I need to get these text fields and even if any of the field is empty, I need to run a search query in database via controller and return set of results in a new page. I tried to store the result in array list of ArrayList but it didnt work. Could anyone please help me?</p>
  
  <p>I am trying to get the text values from from web page in controller:</p>
  
  <pre><code>String lastname = request.getParameter("lastname");
  String firstname = request.getParameter("firstname");
  String gender = request.getParameter("gender");
  String speciality = request.getParameter("speciality");
  String keyword = request.getParameter("keyword");
  </code></pre>
  
  <p>I have Doctor's table which has all these texts fields stored. Lastname, firstname, gender, speciality. I have a class named doctor which links to the database.</p>
  
  <pre><code>@Entity 
  @Table(name="DOCTOR_DETAILS")
  public class Doctor {
  
    @Id
    @Column (nullable=false)
    private int npi;
    @Column (nullable=false)
    private String firstName;
    @Column (nullable=false)
    private String lastName;
    @Column (nullable=false)
    private String gender;
    @Column (nullable=false)
    private String speciality;
    public int getNpi() {
      return npi;
    }
    public void setNpi(int npi) {
      this.npi = npi;
    }
    public String getFirstName() {
      return firstName;
    }
    public void setFirstName(String firstName) {
      this.firstName = firstName;
    }
    public String getLastName() {
      return lastName;
    }
    public void setLastName(String lastName) {
      this.lastName = lastName;
    }
  
    public String getGender() {
      return gender;
    }
    public void setGender(String gender) {
      this.gender = gender;
    }
  
    public String getSpeciality() {
      return speciality;
    }
    public void setSpeciality(String speciality) {
      this.speciality = speciality;
    } 
  
  }
  </code></pre>
  
  <p>I need to run a select query to retrieve the records from table. Even if any of the 4 fields are empty, the query should retrieve the records.</p>
  
  <pre><code>try {
    begin();
    ArrayList &lt;String&gt; cntList = new ArrayList&lt;String&gt;();
    Query q = getSession().createQuery("select * from   
    doctor where lastName = :lastName or firstName =:firstName or    
    gender=:gender or speciality = :speciality");
    q.setString("username", username);
    cntList = (ArrayList&lt;String&gt;) q.list();
    commit();
    return cntList;
  }    
  </code></pre>
  
  <p>I tried the below way of using Hibernate criteria, but I am getting an error. Am I going wrong anywhere?.</p>
  
  <p>I wrote below code in my controller:</p>
  
  <p><img src="https://i.stack.imgur.com/JtUHL.png" alt="enter image description here"></p>
  
  <p>Below part in my DAO class which connects to the database.</p>
  
  <p><img src="https://i.stack.imgur.com/sAVqW.png" alt="enter image description here"></p>
  
  <p>But I am  getting below error:</p>
  
  <p><img src="https://i.stack.imgur.com/jRk3V.png" alt="enter image description here"></p>
  
  <p>Could you please help me out?</p>
  
  <p>The code worked fine and returned the list of results. I tried to follow the steps provided in tutorial point site, but I am getting a cast error.
  "Type mismatch: cannot convert from int to doctor.</p>
  
  <p>A type mismatch asking me to change 
  Doctor doctor = iterator.next();
  to 
  int doctor = iterator.next();</p>
  
  <p>which is something weird, since I cannot retrieve values from Doctor class. </p>
  
  <p><img src="https://i.stack.imgur.com/QfqH7.png" alt="enter image description here"></p>