Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Using database defaults
Using database defaults [message #558160] Fri, 10 September 2010 07:26 Go to next message
Eclipse UserFriend
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 14:26 Go to previous messageGo to next message
Eclipse UserFriend
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.

Re: Using database defaults [message #559590 is a reply to message #558634] Fri, 17 September 2010 05:45 Go to previous message
Eclipse UserFriend
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: Sun Jul 06 02:20:16 EDT 2025

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

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

Back to the top