samedi 11 avril 2015

How to select multiple records from table in hibernate

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.


enter image description here


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?


I am trying to get the text values from from web page in controller:



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");


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.



@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;
}

}


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.



try {
begin();
ArrayList <String> cntList = new ArrayList<String>();
Query q = getSession().createQuery("select contactName,comments from
Contacts where lastName = :lastName or firstName =:firstName or
gender=:gender or speciality = :speciality");
q.setString("username", username);
cntList = (ArrayList<String>) q.list();
commit();
return cntList;
}


Also I am not sure whether I will be able to store the result in array list if I specify it as String. Could anyone please help me?


Aucun commentaire:

Enregistrer un commentaire