Setting a JPA timestamp column to create a database?

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

I use JPA entity classes created with Netbeans 6.5 and have this in my code

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

However, when I try to put an object in the database, I get

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

I tried setting the @Basic parameter to (optional = true) , but this throws an exception because the database does not allow null values ​​for the TIMESTAMP column, which is not by design.

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

I used to get this to work in pure Hibernate, but I have the point of switching to JPA and don’t know how to say that this column is supposed to be generated on the database side. Note that I am still using Hibernate as the persistence level of JPA.

+52
java timestamp annotations jpa persistence
May 01 '09 at 3:20 p.m.
source share
7 answers

I fixed the problem by changing the code to

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

Thus, timestamp columns are ignored when generating SQL inserts. Not sure if this is the best way to do this. Feedback is welcome.

+50
May 01 '09 at 18:57
source share

I understand that this is a bit late, but I had success with annotating the timestamp column with

 @Column(name="timestamp", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP") 

This should also work with CURRENT_DATE and CURRENT_TIME . I am using JPA / Hibernate with Oracle, therefore YMMV.

+35
Jun 24 '10 at 6:20
source share
 @Column(nullable = false, updatable = false) @CreationTimestamp private Date created_at; 

it worked for me. more information

+6
Dec 29 '17 at 9:27
source share

This works fine for me using JPA2.0 and MySQL 5.5.10, for cases where I only care about the last row change. MySQL will create a timestamp on the first insertion and every time UPDATE is called on a row. (NOTE: this will be problematic if I care if UPDATE really made the change).

The "timestamp" column in this example is similar to the "last-touched" column.x`

The code below uses a separate “version” column for optimistic locking.

 private long version; private Date timeStamp @Version public long getVersion() { return version; } public void setVersion(long version) { this.version = version; } // columnDefinition could simply be = "TIMESTAMP", as the other settings are the MySQL default @Column(name="timeStamp", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") @Temporal(TemporalType.TIMESTAMP) public Date getTimeStamp() { return timeStamp; } public void setTimeStamp(Date timeStamp) { this.timeStamp = timeStamp; } 

(NOTE: @Version does not work in the MySQL “DATETIME” column, where the attribute type is “Date” in the Entity class. This was because the date generated the value before the millisecond, however MySQL did not save the millisecond, so when it did the comparison between with what was in the database and the “attached” entity, he thought they had different version numbers)

From the MySQL manual regarding TIMESTAMP :

 With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. 
+4
Apr 28 '11 at 3:22
source share

I do not think that each database has an automatic update of timestamps (for example, Postgres). So I decided to update this field manually everywhere in my code. This will work with every database:

 thingy.setLastTouched(new Date()); HibernateUtil.save(thingy); 

There are reasons to use triggers, but for most projects this is not one of them. Triggers dig you even deeper into a specific database implementation.

MySQL 5.6 .28 (Ubuntu 15.10, OpenJDK 64-bit 1.8.0_66) seems to be very forgiving, requiring nothing but

 @Column(name="LastTouched") 

MySQL 5.7 .9 (CentOS 6, OpenJDK 64-bit 1.8.0_72) only works with

 @Column(name="LastTouched", insertable=false, updatable=false) 

not

 FAILED: removing @Temporal FAILED: @Column(name="LastTouched", nullable=true) FAILED: @Column(name="LastTouched", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") 

My other system information (identical in both environments)

  • hibernate-entitymanager 5.0.2
  • hibernate-validator 5.2.2
  • mysql-connector-java 5.1.38
+4
Feb 15 '16 at 19:49
source share

This works fine for me using JPA 2.0 and MySQL 5.5.10.

I inserted a Timesamp field as follows:

 @Column(name = "LastTouched") private Timestamp lastTouched; 
0
Apr 03 '15 at 9:07
source share
 @Column(name = "LastTouched", insertable = false, updatable = false, columnDefinition = "TIMESTAMP default getdate()") @Temporal(TemporalType.TIMESTAMP) private Date LastTouched;'enter code here' 
0
Jun 17 '19 at 10:36
source share



All Articles