【问题标题】:Grails domainClasses get transient propertiesGrails domainClasses 获取瞬态属性
【发布时间】:2011-09-16 19:27:40
【问题描述】:

GrailsDomainClass 类有两个方法:getPropertiesgetPersistentProperties

我有一个域类 (MyDomainClass),其中包括:

static transients = {computeStuff}
float computeStuff(){
  def thisCouldChange = 3.1
  return thisCouldChange  //it actually does things but not of consequence to my question
}

好的,我把默认索引页面修改为列出MyDomainClass的所有属性,如下所示:

<g:each var="d" in="${grailsApplication.domainClasses.sort { it.fullName } }">
<h2>${d.fullName}</h2>
<g:each var="f" in="${d.properties.sort { it.fieldName } }">
<br>${f.fieldName }
</g:each>
</g:each>

好的。这行得通,但它没有得到任何瞬态属性。我已经尝试过 d.properties 和 d.persistantProperties ,它们似乎给了我相同的结果。提前感谢您的帮助!

我需要将其称为 getComputeStuff 吗?

我现在已经更改了我的域类以包含它,但仍然没有取回瞬态 computeStuff

static transients = ['computeStuff']
float getComputeStuff(){
  def thisCouldChange = 3.1
  return thisCouldChange  //it actually does things but not of consequence to my question
}

这似乎没什么区别。

【问题讨论】:

  • 您是否尝试过将 computeStuff 定义为闭包而不是方法 float computeStuff = { ... } ?我不确定这是否会影响它。
  • 所以 float computeStuff 惹恼了 SessionFactory,但 def computeStuff 让我回到了开始的地方。

标签: grails metaprogramming grails-domain-class transient


【解决方案1】:

我认为,如果您希望将 computeStuff 方法视为属性,请将其称为 getComputeStuff - 命名约定为 JavaBeans。我不确定它是否会起作用,但我认为值得一试;)

【讨论】:

  • 值得一试,但不成功。尽管如此,还是感谢您的意见。请参阅上面的我的新域代码段。
【解决方案2】:

移除静态瞬态减速。如下定义您的方法:

def getComputeStuff(){
  def thisCouldChange = 3.1
  return 3.1  //it actually does things but not of consequence to my question
}

之后,属性调用“computeStuff”应该出现在您的属性列表中,调用域类上的 getProperties()。将返回值定义为 def 非常重要。

【讨论】:

    猜你喜欢
    • 2012-07-03
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2011-12-23
    • 2011-02-10
    • 2023-03-30
    相关资源
    最近更新 更多