views:

169

answers:

1

Does Grails static 'mapping' property in Domain classes violate DRY? Let's take a look at the canonical domain class:

class Book {
     Long id
     String title
     String isbn
     Date published
     Author author
     static mapping = {
            id generator:'hilo', params:[table:'hi_value',column:'next_value',max_lo:100]
     }
}

or:

class Book { ...
        static mapping = {
            id( generator:'sequence', params:[sequence_name: "book_seq"] )
    }
}

And let us say, continuing this thought, that I have my Grails application working with HSQLDB or MySQL, but the IT department says I must use a commercial software package (written by a large corp in Redwood Shores, Calif.).

Does this change make my web application nobbled in development and test environments? MySQL supports autoincrement on a primary key column but does support sequence objects, for example.

Is there a cleaner way to implement this sort of 'only when in production mode' without loading up the domain class?

A: 

This question (re: sequences) is more related to Hibernate than Grails -- and is well documented on our site

There are typically several reasons that the 'IT department says I must use a commercial software package'.... Among these are naming standards for database objects such as views, tables, even columns.

Another reason would be a standard such as 'every table must have its own sequence' that is a common (oft over-used) practice to assure ids for objects fall in certain ranges. To accomplish this you could add a static mapping block for the renames, e.g. COMMENT becomes COMMENT_TBL and subclass the Hibernate Dialect for the database you are using.


N.B.: ORACLE-specific id generator in mapping closure are now ignored by the Grails 1.2.x framework, so this question is not relevant to recent versions. See JIRA for some background on this question - and orig recommendation here. Keep in mind if you just wanted the one-sequence-per-table approach you could only use the subclass Oracle10gDialect as recommended by the hibernate community , specifically Rob Hasselbaum:

public class TableNameSequenceGenerator extends SequenceGenerator {

/**
 * If the parameters do not contain a {@link SequenceGenerator#SEQUENCE} name, we
 * assign one based on the table name.
 */
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
    if(params.getProperty(SEQUENCE) == null || params.getProperty(SEQUENCE).length() == 0) {
        String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
        if(tableName != null) {
            String seqName = “seq_” + tableName;
            params.setProperty(SEQUENCE, seqName);               
        }
    }
    super.configure(type, params, dialect);
}

}

public class MyDialect extends Oracle10gDialect {
   public Class getNativeIdentifierGeneratorClass() {
      return TableNameSequenceGenerator.class;
   }
}
mikesalera