views:

6229

answers:

5

In my SQL Server 2000 database, I have a timestamp (in function not in data type) column of type datetime named lastTouched set to (getdate()) as its default value/binding.

I am using the Netbeans 6.5 generated JPA entity classes, and have this in my code

@Basic(optional = false)
@Column(name = "LastTouched")
@Temporal(TemporalType.TIMESTAMP)
private Date lastTouched;

However when I try to put the object into the database I get,

javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.generic.Stuff.lastTouched

I've tried setting setting the @Basic to (optional = true), but that throws a exception saying the database doesn't allow null values for the timestamp column, which it doesn't by design.

ERROR JDBCExceptionReporter - Cannot insert the value NULL into column 'LastTouched', table 'DatabaseName.dbo.Stuff'; column does not allow nulls. INSERT fails.

I previously got this to work in pure Hibernate, but I have sense switched over to JPA and have no idea how to tell it that this column is suppose to be generated on the database side. Note that I am still using Hiberate as my JPA persistance layer.

A: 

private Date lastTouched = new Date();

zmf
That creates it on the java side though, I really want the database to generate the timestamp, as it will be more accurate.
James McMahon
it sounds like you don't want to, but you could just programmatically set lastTouched. If you had this working in hibernate before you can just use that hibernate mapping.
zmf
+3  A: 

I fixed the issue by changing the code to

@Basic(optional = false)
@Column(name = "LastTouched", insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastTouched;

So the timestamp column is ignored when generating SQL inserts. Not sure if this is the best way to go about this. Feedback is welcome.

James McMahon
You may also want to add @Column(name = "LastTouched", insertable = false, updatable = false)to use database generated timestamps also with SQL UPDATE statements.
Juha Syrjälä
Thanks Juha. I'm sure that would have messed me up later down the road.
James McMahon
Actually, thinking about it more, I think I will need the column on update, as I am going to need to update it through Java when I make column changes. Unless there is a way to do the column update on the database side somehow.
James McMahon
According to http://stackoverflow.com/questions/36001/sql-server-2005-auto-updated-datetime-column-lastupdated, a trigger is what I should be using here.
James McMahon
A: 

I realize this is a bit late, but I've had success with annotating a timestamp column with @Column(name="timestamp" columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP"). This should also work with CURRENT_DATE and CURRENT_TIME. I'm using JPA/Hibernate with Oracle, so YMMV.

Matt Luongo
A: 

Hello!

I have a question.. hmm this code set automatically the timestamp in the column or I need put it with java code?

aaaa
My code I posted as an answer to my own question does not set the timestamp. It essentially makes Java and persistence layer ignore the column.
James McMahon
A: 

Hello,

i use the approach with

@org.hibernate.annotations.Generated(@GenerationTime.INSERT)
@Column(...insertable=false, updatable=false)
private Timestamp created;

which allows the database generation after every insert.

My Problem is now that i only want a database generation if created is not set. The option with @PrePersist seems not work for me because the client and the database are running in different timezones. Especially the database is synchronized with a timeserver and the exact timestamp is crucial.

Thanks in advance Alex

Alex