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

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

问题描述

域类中的 Grails 静态映射"属性是否违反 DRY?我们来看看规范域类:

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

课本{
    长 ID
    字符串标题
    字符串 isbn
    发布日期
    作者 作者
    静态映射 = {
         id 生成器:'hilo', params:[table:'hi_value',column:'next_value',max_lo:100]
    }
}

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

或:

class Book { ...
       静态映射 = {
         id(generator:'sequence', params:[sequence_name: "book_seq"])
    }
}

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

让我们继续这个想法,我的 Grails 应用程序可以使用 HSQLDB 或 MySQL,但 IT 部门说我必须使用商业软件包(由加利福尼亚州红木海岸的一家大型公司编写).

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.).

此更改是否会使我的 Web 应用程序在开发和测试环境中脱颖而出?例如,MySQL 支持对主键列进行自增,但支持序列对象.

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?

推荐答案

这个问题(re: 序列)比 Grails 更与 Hibernate 相关——并且在 我们的网站

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

IT 部门说我必须使用商业软件包"通常有几个原因……其中包括数据库对象(例如视图、表甚至列)的命名标准.

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.

另一个原因是标准,例如每个表必须有自己的序列",这是一种常见(经常被过度使用)的做法,以确保对象的 id 落在特定范围内.为此,您可以为重命名添加一个静态映射块,例如COMMENT 变为 COMMENT_TBL 并为您正在使用的数据库子类化 Hibernate Dialect.

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.

注意:映射闭包中特定于 ORACLE 的 id 生成器现在被 Grails 1.2.x 框架忽略,所以这个问题与最近的版本无关.请参阅 JIRA有关此问题的一些背景信息 - 和原始建议 此处.请记住,如果您只想要每表一个序列的方法,您只能使用 hibernate 社区,特别是 Rob Hasselbaum:

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天全站免登陆