Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Using database defaults
Using database defaults [message #558160] Fri, 10 September 2010 11:26 Go to next message
Thomas Haskes is currently offline Thomas Haskes
Messages: 147
Registered: July 2009
Senior Member
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 #558634 is a reply to message #558160] Mon, 13 September 2010 18:26 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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.



James : Wiki : Book : Blog : Twitter
Re: Using database defaults [message #559590 is a reply to message #558634] Fri, 17 September 2010 09:45 Go to previous message
Thomas Haskes is currently offline Thomas Haskes
Messages: 147
Registered: July 2009
Senior Member
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.
>
>
Previous Topic:EclipseLink + OSGi
Next Topic:EclipseLink 2.1.0 - timeout on idle connections
Goto Forum:
  


Current Time: Fri Oct 31 10:14:33 GMT 2014

Powered by FUDForum. Page generated in 0.02620 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software