2009-10-16 78 views
0

A有两个实体。例如计时设置和订单。HQL是否可以减少或总计日期字段?

@Entity 
public class TimingSettings{ 

    @Basic 
    private Long orderTimeout = 18000; //for example 18000 sec 
    .... 
} 


@Entity 
public class Order{ 

    @Basic 
    @OneToOne 
    private TimingSettings timingSettings; 

    @Temporal(value = TemporalType.TIMESTAMP) 
    @Column(nullable = false) 
    protected Date time; 
    .... 
} 

我不能选择超时之前计算所需的订单时间,因为我不知道什么订单有什么时间设置。

唧唧我可以执行HQL一样如下:

select o from order o 
left join o.timingSettings ts 
where o.time < current_timestamp()+ ts.orderTimeout 

回答

2

这取决于您是否需要它来跨所有数据库工作。

在后一种情况下(特定数据库),检查您的方言是否有可用的日期功能。 MySQL的话,例如,支持timediff()time_to_sec()功能,这将允许您为编写查询:

select o from order o 
    left join o.timingSettings ts 
where time_to_sec(timediff(o.time, current_timestamp())) < ts.orderTimeout 

如果你需要做这对于所有数据库(或者,如果不支持需要你的数据库日期功能),Hibernate支持所有方言的year(), month(), ... second()功能。使用这些将使您的查询更加冗长:-)另外,您将遇到DST问题。