2011-02-06 53 views
2

我有一个类拍卖喜欢限制在HQL与日期

public class Auction extends LightEntity implements IsSerializable 
{ 
    private long AuctionId; 
    private Date StartTime; 
    private Date EndTime; 
} 

我需要执行编写使用限制其选择全部拍卖,其结束时间还未完成的HQL。

的拍卖类我的XML映射文件是

<hibernate-mapping> 
    <class name="com.BiddingSystem.Models.Auction" table="AUCTION"> 
     <id name="AuctionId" type="long"> 
      <column name="AUCTIONID" /> 
      <generator class="native" /> 
     </id> 
     <property name="StartTime" type="java.util.Date"> 
      <column name="STARTTIME" /> 
     </property> 
     <property name="EndTime" type="java.util.Date"> 
      <column name="ENDTIME" /> 
     </property> 
.. 
. 
. 
. 
> 

回答

6

首先,你一定要尊重的Java命名约定:变量和字段开始在Java(auctionId,开始时间小写字母,时间结束)。

现在你的问题: 要么你计算当前的时间,并作为参数传递到您的查询:

Date now = new Date(); 
Query q = session.createQuery("select a from Auction a where a.endTime > :now"); 
q.setTimeStamp("now", now); 
return q.list(); 

,或者你使用expressions supported by HQL

Query q = session.createQuery("select a from Auction a where a.endTime > current_timestamp()"); 
return q.list();