标签: grails-domain-class

属性文件中的Grails域类属性

在我的grails应用程序中,我想从属性文件中读取一些值,并在启动时将其设置为Grails Domain类的静态属性.

Class A{

  static myValues="1,2";
} 

 class B{
   static myValues="2,3";
  }
Run Code Online (Sandbox Code Playgroud)

在上面的例子中我直接给出了输入..而不是我想从一个config.properties文件中读取它,它将具有以下内容

A = 1,2-

B = 2,3

是否有可能在grails中这样做.请帮助我.

grails properties grails-domain-class

1
推荐指数
1
解决办法
1488
查看次数

是否有Grails域对象的禁用方法名称列表?

通常当我向域对象添加辅助方法时,我在编译时遇到错误,解析为"找不到x属性".这似乎发生在方法名称上getX,setX也是最近发生的isX.我应该避免使用名单的清单吗?有没有办法注释或以其他方式标记这些方法,所以Grails不会将它们与自动属性混淆?

grails grails-domain-class

1
推荐指数
1
解决办法
569
查看次数

Grails挂钩到GORM beforeUpdate()

我对嵌套域类有内部要求,我希望将父关系的更新传播给子项.代码示例可以说清楚:

class Milestone {
    static belongsTo = [project:Project]
    static hasMany = [goals:OrgGoals, children:Milestone]
    String name
    Date start
    Date estimatedEnd
    Date achievedEnd
    ...
}
Run Code Online (Sandbox Code Playgroud)

当父里程碑的estimatedEnd更新时,我希望孩子的估计会自动更新相同的金额.GORM的beforeUpdate()钩子似乎是一个合乎逻辑的地方:

为了让生活更轻松,我想使用一些简单的Date算法,所以我在Milestone类中添加了以下方法:

def beforeUpdate()
    {
        // check if an actual change has been made and the estimated end has been changed
        if(this.isDirty() && this.getDirtyPropertyNames().contains("estimatedEnd"))
        {
            updateChildEstimates(this.estimatedEnd,this.getPersistentValue("estimatedEnd"))
        }
    }

private void updateChildEstimates(Date newEstimate, Date original)
    {
        def difference = newEstimate - original
        if(difference > 0)
        {
            children.each{ it.estimatedEnd+= difference }
        }
    }
Run Code Online (Sandbox Code Playgroud)

没有编译错误.但是当我运行以下集成测试时:

void …
Run Code Online (Sandbox Code Playgroud)

grails hibernate grails-orm grails-domain-class

1
推荐指数
1
解决办法
5394
查看次数

Grails找到Domain然后保存它

我想要做的是找到一个域,然后创建一个域或保存预先存在的域.这是我目前正在使用的代码(在此项目中,skeleton是包名称):

    def save() {
        Class lob = grailsApplication.getDomainClass('skeleton.'+params.lob.name)
        def instance = lob.get(params.lob.id)
        if (instance){
            params.data.each { name, value ->
                if (instance.metaClass.hasProperty(name)){
                    instance[name] = value
                }
            }
        }else{
            instance = new lob()
            params.data.each { name, value ->
                if (instance.metaClass.hasProperty(name)){
                    instance[name] = value
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用.任何人都可以帮我解决这个问题吗?

grails grails-domain-class

1
推荐指数
1
解决办法
360
查看次数

Grails"在发生异常后不会刷新会话"错误消息

我看到这个错误消息已经在hibernate的上下文中多次发布了.

我在使用grails服务和域类时遇到此错误,任何帮助都将非常感激

域类

class Coupon {
    Date dateCreated
    Date lastUpdated
    String code
    String email
    String address
    String state
    String city
    String zip
    def couponCodeGeneratorService

    def beforeValidate() {
        println code+"---------8-"
        code = couponCodeGeneratorService.generate()
        println code+"----------"
    }
    static constraints = {
        email blank:false,email:true
        address blank:false
        state blank:false
        city blank:false
        zip blank:false
    }
}
Run Code Online (Sandbox Code Playgroud)

服务

class CouponCodeGeneratorService {
    Random randomGenerator = new Random()
    def serviceMethod() {

    }
    def generate(){
        def group1 =  randomGenerator.nextInt(9999)+"";
        def group2 =  randomGenerator.nextInt(9999)+"";
        def group3 =  randomGenerator.nextInt(9999)+"";
        def group4 …
Run Code Online (Sandbox Code Playgroud)

grails groovy grails-domain-class

1
推荐指数
1
解决办法
5813
查看次数

使用params的grails域类.list()方法

我有两个与一对多关系相关的域 - 一个用户可能有很多关联.我想查看属于该用户的所有关联.我正在使用脚手架插件.因此,应该返回AssociationController中的关联列表的代码看起来很简单:

def index(Integer max) {
    respond Association.list(params), model:[associationInstanceCount: Association.count()]
}
Run Code Online (Sandbox Code Playgroud)

在用户视图页面上,我有以下代码:

<g:form controller="Association" >
    <fieldset class="buttons">
        <g:hiddenField name="user.id" id="user.id" value="${userInstance.id}"/>
        <g:actionSubmit class="list" action="index" value="${message(code: 'default.list.label', default: 'Show Associations')}"/>
    </fieldset>
</g:form>
Run Code Online (Sandbox Code Playgroud)

当我按下这个动作user.id提交与请求一起发送并且它在params地图内(我用debug检查)但由于某种原因Association.list(params)返回所有关联(对于所有用户),尽管我在params中使用此user.id.A也尝试重命名user.id为just user,它也没有用.看起来像这样.list()应该只用于排序或者我做错了.你能帮我解决这个问题吗?谢谢!

grails grails-orm grails-domain-class

1
推荐指数
1
解决办法
9368
查看次数

限制域列表上的唯一约束

如果我有一个域Person和一个Domain Hobby,并且Person具有很多Hobby,那么如何确保同一Hobby不会多次添加到Person集合中。

即像

`

   class Hobby {
      String name
      static belongsTo = [person: Person] 
   }
   class Person
      String name

      static hasMany =[hobby: Hobby]

      static constraints= {
        hobby.name unique: true           //like this
      }
   }   
Run Code Online (Sandbox Code Playgroud)

grails grails-domain-class

1
推荐指数
1
解决办法
432
查看次数

查找是否添加到域类中 - 不使用findBy

我有域类=>多对多,并使用addTo关联它们.

class Books {
    static belongsTo = Author
}

class Author {
    static belongsTo = Book
    static hasMany = [books: Book]
}
Run Code Online (Sandbox Code Playgroud)

控制器:

author.addToBooks(mybook).save()
Run Code Online (Sandbox Code Playgroud)

=>我想检查mybook是否已添加到作者.是一种检查书籍是否已添加到作者(希望不使用findBy)的方法吗?就像是

if (author.isAdded(mybook))   
Run Code Online (Sandbox Code Playgroud)

谢谢.

grails grails-domain-class

0
推荐指数
1
解决办法
79
查看次数

Grails域类失败

课程如下:

package tsg

class Country {

    String code
    String codeShort
    String name
    String en
    String nl
    String de
    String fr
    String it
    String es

    static mapping = {
        id name: "code", generator: "assigned"
        version 'revision_number'
    }

    static constraints = {
        code maxSize: 4
        codeShort nullable: true, maxSize: 2
        name nullable: true, maxSize: 100
        en nullable: true, maxSize: 50
        nl nullable: true, maxSize: 50
        de nullable: true, maxSize: 50
        fr nullable: true, maxSize: 50
        it nullable: true, maxSize: 50
        es nullable: …
Run Code Online (Sandbox Code Playgroud)

grails constraints grails-domain-class

0
推荐指数
1
解决办法
435
查看次数

Grails findBy没有使用Or/And Condition的组合

我正在使用Grails应用程序.我正在为我的grails应用程序使用Apache Shiro安全性插件.我试图使用OrAnd条件findBy在我的User域类上使用查询,但它给了我一个错误.

我的域类

class ShiroUser {
      String firstName
      String lastName
      String username
      String email
      String passwordHash
      String userStatus

      static hasMany = [ roles: ShiroRole, permissions: String ]

      static constraints = {
          username(nullable: false, blank: false, unique: true)
          email(nullable: false, blank: false, email: true,unique: true)
     }}
Run Code Online (Sandbox Code Playgroud)

我执行了以下查询:

ShiroUser.findByUsernameOrEmailAndUserStatus(params?.username,params?.username,'Active')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Message: No property found for name [usernameOrEmail] for class [class com.chatportal.ShiroUser]
Run Code Online (Sandbox Code Playgroud)

但是,如果我只用Or条件执行查询,那么它工作正常.

ShiroUser.findByUsernameOrEmail(params?.username,params?.username)
Run Code Online (Sandbox Code Playgroud)

任何人都请帮助我,什么是错我的病情,当我用或者条件与findBy?

grails grails-orm shiro grails-domain-class grails-2.0

0
推荐指数
1
解决办法
714
查看次数