hibernate 查询时 对日期的比较

http://www.roseindia.net/hibernate/hibernate-between-date.shtml  
Hibernate's Built-in criterion: Between (using with Date) 
In this section, you will learn to use "between" i.e.one of the built-in hibernate criterions. Restriction  class  provides built-in criterion via static factory methods. One important  method of the Restriction class is between : which is used to apply a "between" constraint to the named property 

In this tutorial, "Between" is used with the date object. It takes three parameters e.g.  between("property_name",startDate,endDate) 

Here is the code of the class using "between" with the Date class : 
package roseindia.tutorial.hibernate; 

Java代码   收藏代码
  1. import org.hibernate.*;  
  2. import org.hibernate.criterion.*;  
  3. import org.hibernate.cfg.*;  
  4.   
  5. import java.text.DateFormat;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.*;  
  8. /** 
  9.  * @author Deepak Kumar 
  10.  *  
  11.  * http://www.roseindia.net  
  12. Hibernate Criteria Query Example 
  13.  *   
  14.  */public class HibernateCriteriaQueryBetweenDate {  
  15.   public static void main(String[] args) {  
  16.     Session session = null;  
  17.     try {  
  18.       // This step will read   
  19. hibernate.cfg.xml and prepare hibernate for  
  20.       // use  
  21.       SessionFactory sessionFactory   
  22. new Configuration().configure()  
  23.           .buildSessionFactory();  
  24.       session = sessionFactory.openSession();  
  25.       //Criteria Query Example  
  26.       Criteria crit =   
  27. session.createCriteria(Insurance.class);  
  28.       DateFormat format =   
  29. new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  30.       Date startDate =   
  31. (Date)format.parse("2005-01-01 00:00:00");  
  32.       Date endDate =   
  33. (Date)format.parse("2005-03-03 00:00:00");  
  34.       crit.add(Expression.between  
  35. ("investementDate"new Date(startDate.getTime()),  
  36.  new Date(endDate.getTime()))); //  
  37. Between date condition  
  38.       crit.setMaxResults(5); //  
  39. Restricts the max rows to 5  
  40.   
  41.       List insurances = crit.list();  
  42.       for(Iterator it =   
  43. insurances.iterator();it.hasNext();){  
  44.         Insurance insurance =   
  45. (Insurance) it.next();  
  46.         System.out.println("  
  47. ID: " + insurance.getLngInsuranceId());  
  48.         System.out.println("  
  49. Name: " + insurance.getInsuranceName());  
  50.         System.out.println("  
  51. Amount: " + insurance.getInvestementAmount());  
  52.         System.out.println("  
  53. Date: " + insurance.getInvestementDate());  
  54.           
  55.       }  
  56.       session.close();  
  57.     } catch (Exception e) {  
  58.       System.out.println(e.getMessage());  
  59.     } finally {  
  60.     }      
  61.   }  
  62. }  

Download this code: 

Output: 
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). 

log4j:WARN Please initialize the log4j system properly. 

Hibernate: select this_.ID as ID0_0_, this_.insurance_name as insurance2_0_0_, this_.invested_amount as invested3_0_0_, 
this_.investement_date as investem4_0_0_ from insurance this_ where this_.investement_date between ? and ? limit ? 



转载于:https://my.oschina.net/admq/blog/202415

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值