Setting database-agnostic default column timestamp using Hibernate
Posted
by unsquared
on Stack Overflow
See other posts from Stack Overflow
or by unsquared
Published on 2010-06-08T18:22:30Z
Indexed on
2010/06/08
20:32 UTC
Read the original article
Hit count: 395
I'm working on a java project full of Hibernate (3.3.1) mapping files that have the following sort of declaration for most domain objects.
<property name="dateCreated" generated="insert">
<column name="date_created" default="getdate()" />
</property>
The problem here is that getdate() is an MSSQL specific function, and when I'm using something like H2 to test subsections of the project, H2 screams that
getdate()
isn't a recognized function. It's own timestamping function is
current_timestamp().
I'd like to be able to keep working with H2 for testing, and wanted to know whether there was a way of telling Hibernate "use this database's own mechanism for retrieving the current timestamp". With H2, I've come up with the following solution.
CREATE ALIAS getdate AS $$ java.util.Date now() { return new java.util.Date(); } $$;
CALL getdate();
It works, but is obviously H2 specific.
I've tried extending H2Dialect and registering the function getdate(), but that doesn't seem to be invoked when Hibernate is creating tables. Is it possible to abstract the idea of a default timestamp away from the specific database engine?
© Stack Overflow or respective owner