Hibernate Criteria:如何为许多搜索字段创建查询? [英] Hibernate Criteria: How to create a query for many search fields?

查看:98
本文介绍了Hibernate Criteria:如何为许多搜索字段创建查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果输入了任何字段,我需要从DB获取详细信息,字段如下所示:


  1. 日期



  2. 动作

  3. li>

    状态


  4. UserName

  5. / li>

请帮助我,因为可以输入任何字段,而不一定需要输入所有值。



谢谢,问候,
Jafer

解决方案

  public List searchAccommodation(Date startDate,Date endDate ,Country country,AccommodationType type,Integer capacity)
Criteria criteria = session.createCriteria(Accommodation.class); $(b)b
if(startDate!= null){
criteria.add(Expression.ge(availabilityDate,startDate);
}
if(endDate!= null ){
criteria.add(Expression.le(availabilityDate,endDate);
}
if(country!= null){
criteria.add(Expression.eq country,country);
}
if(capacity!= null){
criteria.add(Expression.ge(capacity,capacity);
}
if(type!= null){
criteria.add(Expression.eq(type,type);
}
List results = criteria.list();
//
//执行查询
//
返回query.list();
}


I need to fetch details from DB if any of the fields are entered, the fields are as below

  1. Date

  2. Code

  3. Action

  4. Status

  5. UserName

  6. Application.

Kindly help me as any of the fields can be entered and not necessarily all values need to entered.

Thanks & Regards, Jafer

解决方案

public List searchAccommodation(Date startDate, Date endDate, Country country, AccommodationType type, Integer capacity)
    Criteria criteria = session.createCriteria(Accommodation.class);

    if (startDate != null) {
        criteria.add(Expression.ge("availabilityDate",startDate);
    }                
    if (endDate != null) {
        criteria.add(Expression.le("availabilityDate",endDate);
    }                
    if (country != null) {
          criteria.add(Expression.eq("country",country);
    }                
    if (capacity != null) {
          criteria.add(Expression.ge("capacity",capacity);
    }                
    if (type != null) {
          criteria.add(Expression.eq("type",type);
    }                
    List results = criteria.list();
    //
    // Execute the query
    //
    return query.list();
}

这篇关于Hibernate Criteria:如何为许多搜索字段创建查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆