HQL可以减去或汇总日期字段吗?

时间:2009-10-16 10:00:13

标签: hibernate timestamp hql

A有两个实体。例如,时间设置和订单。

@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;
    ....
}

我无法在之前选择超时并计算所需的订单时间,因为我不知道哪些订单有什么时序设置。

Haw我可以执行与以下相同的HQL:

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

2 个答案:

答案 0 :(得分: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问题。

答案 1 :(得分:0)

问题在于算术和时间戳,请查看thread以获取一些想法。

相关问题