在数据库中查找日期对象
Look for a date object in database
我想获取在特定日期发生的所有事件,但我无法让它工作。
Event buffer_event = Event.find.where().like("date",new_date).findUnique();
这里的 new_date 是一个正常的日期对象,日期为 03/03/2016 09:00。
但是当我尝试 运行 这一行时出现错误。
java.util.Date cannot be converted to java.lang.String
所以我用字符串试了一下:
Event buffer_event = Event.find.where().like("date", "03/03/2016 09:00").findUnique();
但我也收到错误消息:
[PersistenceException: Query threw SQLException:Cannot parse "TIMESTAMP" constant "Thu Mar 03 09:00:00 CET 2016"; SQL statement: select t0.id c0, t0.name c1, t0.date c2, t0.user_id c3 from event t0 where t0.date like ? [22007-187] Bind values:[Thu Mar 03 09:00:00 CET 2016] Query was: select t0.id c0, t0.name c1, t0.date c2, t0.user_id c3 from event t0 where t0.date like ? ]
那么如何从我的数据库中获取当天的所有事件?
编辑:
@Entity
public class Event extends Model {
@Id
public Long id;
public String name;
@Formats.DateTime(pattern="dd/MM/yyyy HH:mm")
public Date date;
@ManyToOne()
public User user;
public Event (String name, Date date) {
this.name = name;
this.date = date;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; }
public static Model.Finder<Long,Event> find = new Model.Finder<Long,Event>(Long.class, Event.class);
}
这就是我的活动 class。
# --- Rev:1,Ups - df1003e
create table event (
id bigint not null,
name varchar(255),
date timestamp,
user_id bigint,
constraint pk_event primary key (id))
;
create table user (
id bigint not null,
email varchar(255),
password varchar(255),
role varchar(255),
firstname varchar(255),
lastname varchar(255),
constraint pk_user primary key (id))
;
create sequence event_seq;
create sequence user_seq;
alter table event add constraint fk_event_user_1 foreign key (user_id) references user (id) on delete restrict on update restrict;
create index ix_event_user_1 on event (user_id);
首先,您可能应该使用 .eq
而不是 .like
作为日期。对于查询,您是否尝试将 DateTime
对象传递给查询?像这样的东西(未经测试):
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm");
DateTime dt = formatter.parseDateTime("03/03/2016 09:00");
Event buffer_event = Event.find.where().eq("date", dt).findUnique();
我想获取在特定日期发生的所有事件,但我无法让它工作。
Event buffer_event = Event.find.where().like("date",new_date).findUnique();
这里的 new_date 是一个正常的日期对象,日期为 03/03/2016 09:00。 但是当我尝试 运行 这一行时出现错误。
java.util.Date cannot be converted to java.lang.String
所以我用字符串试了一下:
Event buffer_event = Event.find.where().like("date", "03/03/2016 09:00").findUnique();
但我也收到错误消息:
[PersistenceException: Query threw SQLException:Cannot parse "TIMESTAMP" constant "Thu Mar 03 09:00:00 CET 2016"; SQL statement: select t0.id c0, t0.name c1, t0.date c2, t0.user_id c3 from event t0 where t0.date like ? [22007-187] Bind values:[Thu Mar 03 09:00:00 CET 2016] Query was: select t0.id c0, t0.name c1, t0.date c2, t0.user_id c3 from event t0 where t0.date like ? ]
那么如何从我的数据库中获取当天的所有事件?
编辑:
@Entity
public class Event extends Model {
@Id
public Long id;
public String name;
@Formats.DateTime(pattern="dd/MM/yyyy HH:mm")
public Date date;
@ManyToOne()
public User user;
public Event (String name, Date date) {
this.name = name;
this.date = date;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; }
public static Model.Finder<Long,Event> find = new Model.Finder<Long,Event>(Long.class, Event.class);
}
这就是我的活动 class。
# --- Rev:1,Ups - df1003e
create table event (
id bigint not null,
name varchar(255),
date timestamp,
user_id bigint,
constraint pk_event primary key (id))
;
create table user (
id bigint not null,
email varchar(255),
password varchar(255),
role varchar(255),
firstname varchar(255),
lastname varchar(255),
constraint pk_user primary key (id))
;
create sequence event_seq;
create sequence user_seq;
alter table event add constraint fk_event_user_1 foreign key (user_id) references user (id) on delete restrict on update restrict;
create index ix_event_user_1 on event (user_id);
首先,您可能应该使用 .eq
而不是 .like
作为日期。对于查询,您是否尝试将 DateTime
对象传递给查询?像这样的东西(未经测试):
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm"); DateTime dt = formatter.parseDateTime("03/03/2016 09:00"); Event buffer_event = Event.find.where().eq("date", dt).findUnique();