开发者

How do I write JPA QL statements that hints to the runtime to use the DEFAULT value?

开发者 https://www.devze.com 2022-12-30 02:31 出处:网络
I have a table like so: mysql> show create table foo; CREATE TABLE foo ( network bigint NOT NULL, activeDate datetime NULL default \'0000-00-00 00:00:00\',

I have a table like so:

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

In the domain object, FooVO the activeDate member is annotated as Temporal. If I don't set activeDate to a valid Date instance, a new record is inserted with NULLs. I want the default value t开发者_运维百科o take effect if I don't set the activeDate member.


If you are using EclipseLink then you can specify @ReturnInsert(returnOnly=true) to do this. Unfortunately if you are still using Toplink the only way I have found to deal with this issue is to not specify the @Temporal on your date column and write wrapper methods to convert back and forth from Date to String.


Aren't you supposed to use NOT NULL for the activeDate column if you want the default to get set?

CREATE TABLE foo
(
    network bigint NOT NULL,
    activeDate datetime NOT NULL default '0000-00-00 00:00:00',
    ...
)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号