将Grails静态'映射'属性重载到数据库对象上是否违反DRY? [英] Does overloading Grails static 'mapping' property to bolt on database objects violate DRY?

查看:72
本文介绍了将Grails静态'映射'属性重载到数据库对象上是否违反DRY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails在Domain类中是否存在违反DRY的静态'映射'属性?
让我们来看一下规范的领域类:


class Book {
      Long id

    字符串标题

     String isbn

    发布日期

    作者作者

    静态映射= {
             id generator:'hilo',params:[table:'hi_value' ,列:'next_value',max_lo:100]

     }


或:


class Book {...

         static mapping = {

           id(generator:'sequence',params:[sequence_name:book_seq])
    ; }


让我们说,继续这个想法,我有我的Grails应用程序使用HSQLDB或MySQL,但IT部门表示我必须使用商业软件包(由加利福尼亚州Redwood Shores的一家大公司编写)。



这是否会改变使我的Web应用程序在开发和测试环境中徘徊不前?例如,MySQL支持在主键列上自动增量,但支持序列对象。



是否有一种更简洁的方法来实现这种'仅在生产模式下'加载领域类?

解决方案

这个问题(re:序列)与Hibernate比Grails更相关 - 记录在我们的网站



IT部门说我必须使用商业软件包......通常有几个原因,其中包括数据库对象的命名标准,如视图,表格,甚至是列。另一个原因是每个表格都必须有自己的序列这个标准,这是一种常见的(经常使用的)惯例,以确保对象的ID落入一定范围。
为了达到这个目的,你可以为重命名添加一个静态映射块,例如COMMENT成为COMMENT_TBL,并为您使用的数据库子类化Hibernate Dialect。






注意:映射中特定于ORACLE的id生成器闭包现在被Grails 1.2.x框架忽略,所以这个问题与最近的版本无关。有关此问题的背景信息,请参阅 JIRA
- 并推荐 here 。请记住,如果您只想要每个表的一个序列的方法,那么您只能使用hibernate推荐的子类Oracle10gDialect community ,特别是Rob Hasselbaum:

  public class TableNameSequenceGenerator extends SequenceGenerator {

/ **
*如果这些参数不包含序列名称序列号,我们
*根据表名分配一个参数。
* /
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;
}
}


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?

解决方案

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;
   }
}

这篇关于将Grails静态'映射'属性重载到数据库对象上是否违反DRY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆