1

I added a new domain class to my project, and when call the method to add a new instance to the table, throw a strange error.


This is the call: (a simple save and print errors if not)

if (!distributor.save()) {
    flash.error = message(code: 'unexpectedError')
    distributor.errors.each {
        println it  
    }
}

This is the stacktrace: (only the caused by portion)

Caused by: java.sql.SQLException: Field 'version' doesn't have a default value

Does someone got this error? I don't understand why this is happening because version is a auto-generated field by Grails/Hibernate framework.

I tried to delete the table and recreate it but still getting the same error. Could that be an bug? I don't think that i am doing wrong because is not my first table or relationship in the project.

Any help will be appreciated.

IgniteCoders
  • 4,834
  • 3
  • 44
  • 62

2 Answers2

2

If you don't need a version use

static mapping = {
        version false
}

if you do need it, try

static mapping = {
        version true
}

you can set a default value like this

static mapping = {
        PROPERTY_NAME defaultValue: DESIRED_VALUE
}
shaydel
  • 589
  • 4
  • 15
  • 1
    The field version, is created and used by hibernate to handle if the row was updated or deleted by another transaction. In all my other tables (domain classes), i don't handle anything of the version, and always worked fine. **What i want to know is why this is throwing an error now.** Because that field is not recommended to be modified by the programmer, Grails/Hibernate is the responsible of that. – IgniteCoders Dec 16 '15 at 12:07
  • if you use migrations, you will have a lead on who created what and when, and you will see if version is created out-of-the-blue or by gorm. i have encountered that.overall version will effect your optimistic locking mechanism, but it will still work either way, with some performance drawbacks. – shaydel Dec 16 '15 at 12:55
  • Thanks for your answer. I've finally come to the conclusion that if you want to modify something of the domain classes or their relationships, stop the application before do it, to don't spend time trying to fix inexplicable errors caused by application re-compile – IgniteCoders Dec 16 '15 at 14:05
  • @IgniteCoders Now in 2019 I seem to be experiencing this issue. It works wieldly after recompiling. Restarting the application yields rational result. Is it still a Grails/Hibernate bug? – Jack Lu Jun 06 '19 at 09:04
  • As I know, is still a Grails/Hibernate bug – IgniteCoders Jun 06 '19 at 09:13
2

I found a solution that is not the most efficient but solved my problem.

That looks like a Grails/Hibernate bug, that happens sometimes when you add a domain class, or modify it or their relationships while the project is already running.


In this question they ask about the same error, but with the field id. I tried this solution and work:

  1. Drop the table in question.
  2. Make a backup of the database.
  3. Drop the database
  4. Recreate the database from the backup
  5. Run the project to create the table in question

Step 1 and 2 are not needed if you already have a backup.


The problem of this solution is the time if your database is big. The main take around 20-30 min to do the backup or recreate the database from the backup.

Community
  • 1
  • 1
IgniteCoders
  • 4,834
  • 3
  • 44
  • 62