Tuesday, March 17, 2009

inserting hibernate_sequence and current date and time in PostgreSQL

Recently, I need to add a record in a table from a database instance created by Grails.
Grails relies on hibernate to make persistent all domain classes defined in an application. Thus, most of the Grails applications define a hibernate_sequence in their database model.
In particular, I'm working with grails 1.0.3 and PostgreSQL as my back-end.

Now, I needed to insert a new record in one table from my Grails application which also has an  attribute of type 'Date'.

For simplicity assume that my table exhibits the following structure:

Table 'demo'

id -> bigint
date -> timestamp
name -> character varying(255)

     "demo_pkey' PRIMARY KEY, btree(id)

In order to make a proper insertion of a new record, execute the following line:

insert into demo (id,date,name) values (nextval('hibernate_sequence'),now(),'John');

;-) (It works on PostgreSQL, may be possible that other back-ends require a slightly different syntax).


No comments: