当前位置:首页 > Hibernate

hibernate 查询时 对日期的比较

canca18年前 (2008-08-14)Hibernate745
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 查询时 对日期的比较” 的相关文章

如何把Hibernate2.1升级到Hibernate3.0?

1.1 Hibernate API 变化 1.1.1 包名 1.1.2 org.hibernate.classic包 1.1.3 Hibernate所依赖的第三方软件包 1.1.4 异常模型 1.1.5 Session接口 1.1.6 createSQLQuery() 1.1.7 Lifecycle…

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),...…

关于Hibernate3的NativeSQL查询是如此简单

转自:http://blog.donews.com/rory/archive/2006/07/22/973406.aspx 现在的系统中虽然使用了。Hibernate但是没有使用Hibernate的关联关系来进行数据库操作。所有的管理操作都是单独实现的。所以也不能用Criteria.add()这种方…

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…

发表评论

访客

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