1

我有一张这样的桌子:

mysql> show create table foo;
CREATE TABLE foo
(
    network bigint NOT NULL,
    activeDate datetime NULL default '0000-00-00 00:00:00',
    ...
)

在域对象中,FooVO成员activeDate被注释为 Temporal。如果我没有设置activeDate为有效的 Date 实例,则会插入一条带有 NULL 的新记录。activeDate如果我不设置成员,我希望默认值生效。

4

3 回答 3

1

如果您使用的是 EclipseLink,那么您可以指定 @ReturnInsert(returnOnly=true) 来执行此操作。不幸的是,如果您仍在使用 Toplink,我发现处理此问题的唯一方法是不在您的日期列上指定 @Temporal 并编写包装器方法来从 Date 到 String 来回转换。

于 2010-09-27T18:58:11.420 回答
0

Normally it is best to provide your defaults in Java, in your object's constructor. Then you will ensure your object model and database are in synch, and ensure the application sees the correct default when creating new objects.

In EclipseLink you can also use the @ReturnInsert annotation (on Oracle database), by setting it in Java is still recommended,

http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_returninsert.htm#CIHHEGJE

于 2013-01-21T15:14:19.870 回答
0

如果您想设置默认值,您不应该使用NOT NULL该列吗?activeDate

CREATE TABLE foo
(
    network bigint NOT NULL,
    activeDate datetime NOT NULL default '0000-00-00 00:00:00',
    ...
)
于 2010-05-11T15:10:35.280 回答