【发布时间】:2014-01-26 09:38:41
【问题描述】:
我已经看到了几个关于这个问题的帖子,没有一个可以挽救 我的 DomainClass 中有以下内容
def afterInsert() {
elasticSearchService.index(this)
}
其中 elasticsaerch 是一项服务,我已将其添加到静态瞬态列表中。好像调用index方法成功后抛出了这个异常
Message: null id in com.easytha.Student entry (don't flush the Session after an exception occurs)
这是索引方法的代码
def index(object) {
try {
if(object==null) {
throw new NullPointerException()
}
if(!(object instanceof Collection || object instanceof Object[])) {
IndexResponse response = client.prepareIndex(grailsApplication.config.esIndexName, object.getClass().getName(),object.id.toString())
.setSource((object as JSON).toString() )
.execute().actionGet()
println "object indexed successfully"
}else if(object instanceof Collection || object instanceof Object[]) {
for (var in object) {
index(var)
}
}
}catch(e) {
e.printStackTrace();
}
}
“对象索引成功”在控制台中打印。
bootstrap.groovy 有以下内容
Student student4 = new Student(firstName:'Sapan',lastName:'Parikh',email:'sapan.parikh@eclinicalworks.com',password:'test123')
student4.save(failOnError : true)
更新
我尝试了Student.withNewSession { elasticSearchService.index(this) },它奏效了。
【问题讨论】:
-
你在 Bootstrap.groovy 中做什么?您在这里提供该代码
-
@Motilal 添加了引导代码,这是一个简单的保存操作。也没有验证错误,我通过打印 this.errors 来检查它们
-
将刷新添加到您的保存 () 并尝试重新启动您的应用 student4.save(flush:true, failOnError : true)
-
@Motilal nope 仍然是同样的错误。
-
This question 可能会给你一些想法。
标签: grails elasticsearch grails-domain-class grails-services