views:

274

answers:

1

In my database, I have a column type : datetime.
Column data example : 2009-02-03 19:04:23.0

I'm using : SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); to define my date format.

I'm using groovySQL to read tables and for each rows, I'm adding a new SampleJ object (mapped to a new table - SampleJ).

Here my closure :

def sampleQuery(int dataset) {

        sqlModule.eachRow("""
            select b.*
            from dataset a, array_data b
            where dataset_id = "${dataset}"
            and a.array_data_id = b.array_data_id ;""")
            {
              def addSample= new SampleJ(it.toRowResult())
              addSample.id = "${it.array_data_id}" as int
              addSample.dateCreated = dateFormat.parse("${it.date_created}")
              //addSample...(more columns)
              addSample.save()
            }
    }

When I check, my temporary table "SampleJ", all my date does not match with "${it.date_created}".
All dates are set to "new Date()" (to closure execution time).

Via debugger :
"${it.date_created}" is define as "Tue Feb 03 19:04:23 CST 2009", corresponding to (2009-02-03 19:04:23.0). I should have this date !

How can I fix this issue ? I have no error, just wrong dates.
Is there an easy way to define my dates in SampleJ ?
An alternative to addSample.dateCreated = dateFormat.parse("${it.date_created}") ?

+1  A: 

Grails will be setting date_created to the current time by convention, you need to add

static mapping = {
        autoTimestamp false
    }

to SampleJ.

Alternatively, rename dateCreated to something else.

leebutts
You right, my problem is due to dateCreated, by using dateCreatedSample I have no troubles ! GORM has dateCreated and lastUpdated properties, here is my error ! Thanks.
Fabien Barbier