【问题标题】:TransientObjectException when calling findBy... on result from Elastic Search in Grails在 Grails 中的 Elastic Search 结果上调用 findBy... 时出现 TransientObjectException
【发布时间】:2016-06-01 01:01:36
【问题描述】:

我已经使用运行 grails 2.5.0 的 grails 项目成功设置了弹性搜索插件。我可以搜索并获得结果。但是某些操作在结果上是不可能的,比如在一个域上调用一个通过 GORM 的 findBy 与另一个域交互的方法...

给定 Foo 类

class Foo {
   static searchable = true

   def getBars() {
      return Bar.findAllByFoo(this)
   }
}

酒吧类

class Bar {
   Foo foo
   static searchable = true
}

下面的第三行将导致 TransientObjectException

def result = Foo.search("Some parameter")
def foo = result.searchResults.first()
def bars = foo.bars //Fails

例外

object references an unsaved transient instance - save the transient instance before flushing: org.example.Foo. Stacktrace follows:
Message: object references an unsaved transient instance - save the transient instance before flushing: org.example.Foo
Line | Method
->>  105 | methodMissing    in org.grails.datastore.gorm.GormStaticApi

在网上搜索这个问题会得到很多与 GORM 保存操作相关的结果,我猜这不是问题所在。如果对象不是从弹性搜索结果中获取的,这完全没问题,为什么我认为这个问题与插件有关。

def foo = Foo.get(1)
def bars = foo.bars //Works

【问题讨论】:

  • 您使用的是哪个版本的elasticsearch?你在使用grails.org/plugin/elasticsearch 吗?
  • 是的,我正在使用那个插件 ':elasticsearch:0.0.4.6'

标签: grails elasticsearch grails-orm elasticsearch-plugin


【解决方案1】:

查看official documentation:

要映射父/子关系,子元素必须要么 包含父元素作为组件或将其引用为 参考文件。此组件必须映射为 子元素。

例子:

class ParentElement {
…
}

class EmbeddingChild {
    ParentElement parentElement

    static searchable = {
        parentElement parent: true, component: true
    }
}

class ReferencingChild {
    ParentElement parentElement

    static searchable = {
        parentElement parent: true, reference: true
    }
}

还有good example at the end

【讨论】:

  • 谢谢,但我已阅读文档,但找不到任何适用于我的情况的内容。我尝试将static searchable = { foo component: true } 添加到 Bar 类,但没有任何运气。我认为我不能向 Foo 类添加更多内容,因为它不包含任何对 Bar 的引用属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多