本文介绍了如何使用域类字符串名称访问用户定义的Grails项目包名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们试图做的是创建一个包含多个包名的grails应用程序。每个软件包名称将代表不同的业务实体和功能。

在一个grails应用程序项目中基本上是多个grails应用程序,每个应用程序都有自己的包。然后,我们希望根据包名来唯一地为表加上前缀,而不必在域类名前加上字母,或者在每个域类上使用静态映射。 DefaultNamingStrategy 可以很好地处理项目中每个域类的表前缀。



我遇到的问题是访问在 classToTableName 方法。对于传递给 classToTableName 方法的每个域类字符串,我需要用户定义的项目包名来根据包名区分应在表上分配哪个唯一前缀。

为了使用 getArtefacts 或<$ c来访问项目包名,我尝试了几种不同的方法$ c> GroovyClassLoader ,但输出是java.lang或grails.commons包。

任何可以提供的援助都将不胜感激。下面的 CustomNamingStrategy 类已经在项目的groovy文件夹中创建,并在DataSource.groovy文件的hibernate部分中引用。

 类CustomNamingStrategy扩展了DefaultNamingStrategy {
String classToTableName(String className){
def packageName
def prefix =

//如何使用className字符串访问域类的包名称?

if(packageName =='recordretention'){
prefix =Rr
}
className =前缀+ className

covertFromCamelCase (super.classToTableName(className))
}

字符串covertFromCamelCase(字符串输入){
GrailsNameUtils.getNaturalName(输入).replaceAll(\\s, 。_)toUpperCase();


$ / code>


解决方案

After很多搜索,grails 2.x中的Holders类提供了src / groovy文件夹中的类所需的访问。

$ c> Class clazz = Holders.grailsApplication.domainClasses.find {it.clazz.simpleName == className} .clazz

packageName = clazz.getPackage()。getName()


What we are attempting to do is create one grails application with multiple package names. Each package name will represent a different business entity and functionality.

Essentially multiple grails applications in one grails application project with each application being represented by having it's own package.

We will then like to uniquely prefix the tables based on the package name without having to prefix the domain class names or use static mapping on every domain class. The DefaultNamingStrategy works well with handling table prefixing on every domain class in the project.

The issue I am having is accessing the project Package name for each domain class name string that is called in the classToTableName method. For each domain class string that is passed to the classToTableName method, I need the user defined project Package name to differentiate what unique prefix should be assigned on the table based on the package name.

I have tried several different things in order to access the project package name using getArtefacts, or GroovyClassLoader, but the output is java.lang or the grails.commons package.

Any assistance that can be provide would be much appreciated. The CustomNamingStrategy class below has been created in the groovy folder of the project and is referenced in the hibernate section of the DataSource.groovy file.

class CustomNamingStrategy extends DefaultNamingStrategy {
    String classToTableName( String className ) {
        def packageName
        def prefix = ""

        // How do I access the package name of the domain class using className string?

        if (packageName == 'recordretention'){
            prefix = "Rr"
        }
        className = prefix + className

        covertFromCamelCase(super.classToTableName(className))
    }

    String covertFromCamelCase(String input) {
        GrailsNameUtils.getNaturalName(input).replaceAll("\\s", "_").toUpperCase();
    }
}
解决方案

After a lot of searching, it is the Holders class in grails 2.x that provides the access needed for classes in the src/groovy folder.

Class clazz = Holders.grailsApplication.domainClasses.find { it.clazz.simpleName == className }.clazz

packageName = clazz.getPackage().getName()

这篇关于如何使用域类字符串名称访问用户定义的Grails项目包名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:28