简体   繁体   中英

Hibernate Search Criteria

I am trying to develope a simple page of Advance Search in which user can search with any of the following fields

1) Student Roll no 
2) Student First name 
3) Student Last name 
4) Student date of birth

all fields are defined in StudentInformation persistance class.

StudentInformation.java

Public Class StudentInformation { 


private long rollNo;
private String firstName;
private String lastName;
private Date dateOfBirth;


//getter and setters


} 

I have created a value object (AdvanceSearchVo.java) in which i have defined variables which are being entered by the user

advanceSearchVo.setRollNo
advanceSearchVo.setFirstName
advanceSearchVo.setLastName
advanceSearchVo.setDateOfBirth

here I am passing VO object to the DAO layer.

List<StudentInformation> getAllSearchRecords = service.getAdavanceSearchRecords(advanceSearchVo);

On the basis of the entered value "getAllSearchRecords" stores all the results fetched from the DB.

In DAO

@Override
public List<StudentInformation> getAdavanceSearchRecords(AdvanceSearchVo advanceSearchVo) {

        Session session = HibernateUtil.getSessionFactory().openSession();

        Criteria criteria = session.createCriteria(PersonInfoMasterDomain.class);

        if(advanceSearchVo.getRollNo()!=null){

            criteria.add(Restrictions.eq("rollNo",advanceSearchVo.getRollNo()));

        }

        if(advanceSearchVo.getFirstName()!=null){

            criteria.add(Restrictions.eq("firstName",advanceSearchVo.getFirstName()));

        }

        if(advanceSearchVo.getLastName()!=null){

            criteria.add(Restrictions.eq("lastName",advanceSearchVo.getLastName()));

        }


        if(advanceSearchVo.getDateOfBirth()!=null){

            criteria.add(Restrictions.eq("dateOfBirth",advanceSearchVo.getDateOfBirth()));

        }

}

When I search on the basis of rollno (only specify one condition), i am getting records. I am not getting records when user enters all the info ie rollno, firstname, lastname and dob or user enters any field other than rollno.

Can anybody tell me where I am going wrong?

Instead of using Restrictions.eq use Restrictions.like in comparing for string. In your case, for the firstname and lastname comparison.

If you don't get any record, it just means that no record in the database matches the criteria. Be aware that equality on strings is case-sensitive and space sensitive: it looks for an exact match. Maybe you have trailing spaces in either your criteria or the names in database.

Similarly, the equality on date is an exact match. If the date is stored as a timestamp in database and is something like 2012-07-22 12:43:05.757657, passing a date of 2012-07-22 00:00:00.000000 will not match what is stored in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM