Using database defaults [message #558160] |
Fri, 10 September 2010 07:26  |
Eclipse User |
|
|
|
Hi,
I'm sorry if this question was asked before, but I couln't find
anything. I'm trying to figure out how to deal with default values set
in the database. For example this entity
public class Entity {
@Id
public String id;
@Column(name="creationDate")
public Date creationDate;
....
}
The column creationDate is not nullable and there is a database default
that uses the current timestamp. Now if I try to persist the entity the
database claims that insertion of null on that field is not permitted,
which is clear, because the EM tries to insert every mapped entity
field. To get the default in this case the EM would have to leave the
column out of the INSERT statement. Is there some sort of annotation or
best-practice what to do in such a case? I read that some people set the
field to not-insertable and not-updateable, but this is not an option
for me, because I still want the field to be editable.
Any ideas?
TIA
Tom
|
|
|
|
Re: Using database defaults [message #559590 is a reply to message #558634] |
Fri, 17 September 2010 05:45  |
Eclipse User |
|
|
|
Thanks,
I will try that out.
Tom
Am 13.09.2010 20:26, schrieb James:
> If you are using Oracle you can use @ReturnInsert in EclipseLink to omit
> the field on insert and get the value back from the database.
>
> Otherwise you could try just insertable=false updateable=true, and call
> refresh if you want the value back after insert.
>
> In general it may be best to put the logic in your object model instead
> of the database, and init the field in the constructor or factory
> instead of defaulting the field in the database.
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.03577 seconds