我正在尝试通过SQL Server 2008中的Hibernate比较时间。
以下代码返回此错误: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;
}
}
1条答案
按热度按时间sxpgvts31#
请参阅以下博客:
https://techcommunity.microsoft.com/t5/sql-server-blog/using-time-and-date-data-types-part-1-it-s-about-time/ba-p/383611
需要将以下内容添加到您的Hibernate连接URL字符串中
我不知道它是真的还是假的,只是玩一下。
sendTimeAsDateTime=false