Search code examples
oracle-databasegrailsgrails-ormh2sequences

Id generation problem in Grails when using Oracle and H2 for the same application


I am working on grails application. I must use H2 for development and Oracle for testing and production. I must use separate sequences for each domain class/table when using Oracle so I used the following in my domain classes:

    static mapping = {
       id (generator:'sequence', params:[sequence:'SOME_SEQUENCE'])
    }

But then I am not able to use H2. I get Unique index or primary key violation error when I try to create new using user interface.

What can be done to have such mapping to work only for production and testing environments and leave defaults for development? I use Grails 1.3.7.


Solution

  • You can embed logic inside the mapping block for cases like this:

    import grails.util.Environment
    
    class MyDomainClass {
       ...
       static mapping = {
          if (!Environment.isDevelopmentMode()) {
             id (generator:'sequence', params:[sequence:'SOME_SEQUENCE'])
          }
       }
    }