Domanda

I have already seen several threads for this issue and none could rescue I have the following in my DomainClass

def afterInsert() {     
        elasticSearchService.index(this)
}

Where elasticsaerch is a service and I have added it to the static transient list. It seems that after calling the index method successfully it throws this exception

Message: null id in com.easytha.Student entry (don't flush the Session after an exception occurs)

This is the code of index method

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();
    }
}

"object indexed successfully" is printed in the console.

The bootstrap.groovy has the following

Student student4 = new Student(firstName:'Sapan',lastName:'Parikh',email:'sapan.parikh@eclinicalworks.com',password:'test123')
    student4.save(failOnError : true)

UPDATE

I tried Student.withNewSession { elasticSearchService.index(this) } which worked.

È stato utile?

Soluzione

It's stabbing at things but maybe shift the save to happening within a transaction:

Student.withTransaction {
  student4.save()
}

I've seen this pop up unexpectedly when doing things outside of services (that should, imo, be in services).

Summing up some subsequent discussion:

The student model was saved throughout the application, so it wasn't suitable to shift all the saves to services or wrap in transaction blocks. The OP notes that moving the original reindexing code into a new session fixed it everywhere.

def afterInsert() {     
    elasticSearchService.index(this)

}

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top