在 mysql 服务器播放框架中存储日期时间

Storing datetime in a mysql server play framework

我有一个连接到 mysql 服务器的 Play 应用程序,在模型中我有一些我想保存为 DateTimes 的属性,但是使用 java.sql.Timestamp 在我的演变中产生属性创建一个表

 create table delivery (
  id                        bigint auto_increment not null,
  deleted                   tinyint(1) default 0,
  description               varchar(500),
  notes                     varchar(1000),
  account_id                bigint,
  customer_id               bigint,
  sender_id                 bigint,
  recipient_id              bigint,
  delivery_status_id        bigint,
  delivery_type_id          bigint,
  package_type_id           bigint,
  item_type_id              bigint,
  call_date                 datetime(6),
  pickup_date               datetime(6),
  delivery_date             datetime(6),
  no_of_pieces              integer,
  cust_type                 integer,
  payment_type              integer,
  way_bill                  integer,
  created_time              date,
  modified_time             date,
  createdby_id              bigint,
  modifiedby_id             bigint,
  constraint pk_delivery primary key (id))

这又会导致 mysql 错误:

We got the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6), modified_time datetime(6), createdby_id bigint, mo' at line 21 [ERROR:1064, SQLSTATE:42000], while trying to run this SQL script:

我了解到是大小参数 (6) 导致了问题,并且能够通过使用 java.sql.Date 来避免它(因为它不会在进化中产生)有没有其他人经历过这个并且知道在这些条件下存储 DateTime 的方法?

我能够通过使用@Column 注释来定义 sql 中的类型来解决这个问题。

    @Column(columnDefinition = "datetime")
    public Timestamp createdAt;

可能我的回答有点晚了,但是你可以尝试使用以下注解:@CreatedTimestamp 或@UpdatedTimestamp。我已经用 Play 测试过了!框架 2.3.x.

@Column(name = "created")
@CreatedTimestamp
protected Date created;

@Column(name = "updated")
@UpdatedTimestamp
protected Date updated;