0

我正在尝试通过 SQL Server 2008 中的休眠来比较时间。

以下代码返回此错误:The data types time and datetime are incompatible in the less than or equal to operator.

crit = session.createCriteria(ObdBlackoutHours.class);
Criterion start = Restrictions.le("blackoutStart", new Date());
        Criterion end = Restrictions.gt("blackoutEnd",new Date());
        List list = crit.add(Restrictions.conjunction().add(start).add(end))
                .list();
        if(list.isEmpty())
            return false;
        else 
            return true;

表设计如下:

CREATE TABLE [dbo].[obd_blackout_hours](
[id] [int] NOT NULL,
[blackout_end] [time](7) NOT NULL,
[blackout_start] [time](7) NOT NULL)

我知道数据库只包含10:17:37,我传递的是这样的东西Thu Nov 14 10:17:37 IST 2013,它无法比较。我测试了相同的代码mysql,似乎工作得很好。但是SQL Server 2008正在制造问题。我也试过通过

currentDate = new SimpleDateFormat("HH:mm:ss").parse(new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()));

new ObdBlackoutHours(1,new Date(),new Date()).getBlackoutStart()

而不仅仅是 Date() 对象。这也失败了。我应该如何比较时间并获得结果。

下面是实体类

@Entity
@Table(name = "obd_blackout_hours", schema = "dbo", catalog = "IVR_Data")
public class ObdBlackoutHours implements java.io.Serializable {

private int id;
private Date blackoutStart;
private Date blackoutEnd;
private Set<Service> services = new HashSet<Service>(0);

public ObdBlackoutHours() {
}

public ObdBlackoutHours(int id, Date blackoutStart, Date blackoutEnd) {
    this.id = id;
    this.blackoutStart = blackoutStart;
    this.blackoutEnd = blackoutEnd;
}

public ObdBlackoutHours(int id, Date blackoutStart, Date blackoutEnd,
        Set<Service> services) {
    this.id = id;
    this.blackoutStart = blackoutStart;
    this.blackoutEnd = blackoutEnd;
    this.services = services;
}

@Id
@Column(name = "id", unique = true, nullable = false)
public int getId() {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}

@Temporal(TemporalType.TIME)
@Column(name = "blackout_start", nullable = false, length = 16)
public Date getBlackoutStart() {
    return this.blackoutStart;
}

public void setBlackoutStart(Date blackoutStart) {
    this.blackoutStart = blackoutStart;
}

@Temporal(TemporalType.TIME)
@Column(name = "blackout_end", nullable = false, length = 16)
public Date getBlackoutEnd() {
    return this.blackoutEnd;
}

public void setBlackoutEnd(Date blackoutEnd) {
    this.blackoutEnd = blackoutEnd;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "obdBlackoutHours")
public Set<Service> getServices() {
    return this.services;
}

public void setServices(Set<Service> services) {
    this.services = services;
}

}
4

1 回答 1

1

参考以下博客:http:
//blogs.msdn.com/b/jdbcteam/archive/2010/04/08/using-time-and-date-data-types-part-1-what-time-is- it.aspx
需要将以下内容添加到您的休眠连接 url 字符串中
我不确定它是否是真/假,只是玩它。
发送时间作为日期时间=假

于 2013-11-14T05:48:59.117 回答