在数据库中查找日期对象

时间:2016-03-06 16:21:31

标签: playframework playframework-2.0 ebean

我希望在特定日期发生所有事件,但我无法让它发挥作用。

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);
}

这是我的活动课程。

    # --- 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);

1 个答案:

答案 0 :(得分:2)

首先,您可能应该使用.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();