当前位置:首页 > Hibernate > 正文内容

hibernate 查询时 对日期的比较

canca17年前 (2008-08-14)Hibernate638
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. }  
import org.hibernate.*; import org.hibernate.criterion.*; import org.hibernate.cfg.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; /** * @author Deepak Kumar * * http://www.roseindia.net Hibernate Criteria Query Example * */public class HibernateCriteriaQueryBetweenDate { public static void main(String[] args) { Session session = null; try { // This step will read hibernate.cfg.xml and prepare hibernate for // use SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory(); session = sessionFactory.openSession(); //Criteria Query Example Criteria crit = session.createCriteria(Insurance.class); DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date startDate = (Date)format.parse("2005-01-01 00:00:00"); Date endDate = (Date)format.parse("2005-03-03 00:00:00"); crit.add(Expression.between ("investementDate", new Date(startDate.getTime()), new Date(endDate.getTime()))); // Between date condition crit.setMaxResults(5); // Restricts the max rows to 5 List insurances = crit.list(); for(Iterator it = insurances.iterator();it.hasNext();){ Insurance insurance = (Insurance) it.next(); System.out.println(" ID: " + insurance.getLngInsuranceId()); System.out.println(" Name: " + insurance.getInsuranceName()); System.out.println(" Amount: " + insurance.getInvestementAmount()); System.out.println(" Date: " + insurance.getInvestementDate()); } session.close(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { } } } 

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 ?

ID: 1

Name: Car Insurance

Amount: 1000

Date: 2005-01-05 00:00:00.0

ID: 4

Name: Car Insurance

Amount: 2500

Date: 2005-01-01 00:00:00.0

ID: 7

Name: Travel Insurance

Amount: 2000

Date: 2005-02-02 00:00:00.0

ID: 8

Name: Travel Insurance

Amount: 600

Date: 2005-03-03 00:00:00.0

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/375.html

标签: Hibernate
分享给朋友:

“hibernate 查询时 对日期的比较” 的相关文章

Hibernate读取Blob类型方法

java 有Blob Clob 类型 像是对其他类型一样写 POJOprivate Blob image; set get........<propery   name="i...

Hibernate + Proxool连接池

费话就不说啦! hibernate.cfg.xml 加入: <property name="hibernate.proxool.pool_alias">dbpool</property>  <property name="hibernate.prox...

关于VO、PO的理解

O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写。通俗点讲,就是将对象与关系数据库绑定,用对象来表示关系数据。在O/R Mapping的世界里,有两个基本的也是重要的东东需要了解,即VO,PO。  VO,值对象(Value Object),PO,持...

java.lang.IllegalAccessError: tried to access method net.sf.ehcache.CacheManager.()V from class org.hibernate.cache.EhCacheProvider

今天把项目从Tomcat下转移到JBOSS了,启动JBOSS服务器之后出了,控制台输出了java.lang.IllegalAccessError: tried to access method net.sf.ehcache.CacheManager.<init>()V from clas...

hibernate's hbm 文件配置

外键:<many-to-one> 被外键引用:<set>...

Hibernate 继承的注解配置

三个类的关系是这样的: BaseObject <- Directory <- Directory 刚开始我在第二个Directory类(称Directory2)上配置@Entity,用Junit测试,报错:No identifier specified for entity:...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。