Details
-
Type:
Bug
-
Status:
Open
-
Priority:
Minor
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Environment:jdk 1.6
gorm-jpa plugin 0.3
Description
Here is my steps:
1. grails install-plugin app-engine and gorm-jap
2. grails create-domain-class myapp.MyDomain
3. grails generate-all myapp.MyDomain
4. grails app-engine
5. visit http://localhost:8080/myDomain/list
6. visit http://localhost:8080/myDomain/create
7. input some value and save
Now I got a exception:
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NullPointerException
at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:54)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:54)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:306)
Caused by: java.lang.NullPointerException
at org.datanucleus.jpa.EntityManagerImpl.find(EntityManagerImpl.java:204)
at org.datanucleus.store.appengine.jpa.DatastoreEntityManager.find(DatastoreEntityManager.java:48)
at org.grails.jpa.JpaPluginSupport$_clinit_closure3_closure6_closure11_closure19.doCall(JpaPluginSupport.groovy:253)
at myapp.MyDomainController$_closure3.doCall(myapp.MyDomainController:18)
at myapp.MyDomainController$_closure3.doCall(myapp.MyDomainController)
... 4 more
But when I revisited the list page of MyDomain, I saw the new item I created was there.
So I tried to edit it and the action was ok.
After I checked the generated controller code, I think the problem was in the save closure:
def save = {
def myDomainInstance = new MyDomain(params)
MyDomain.withTransaction {
if(myDomainInstance.save()) {
flash.message = "MyDomain ${myDomainInstance.id} created" // <== myDomainInstance.id is null
redirect(action:show,id:myDomainInstance.id)
}
else {
render(view:'create',model:[myDomainInstance:myDomainInstance])
}
}
}
I think the reason is when create a new item, if you don't use save(flush: true), you can not always get the id of the new item.
So I changed it to myDomainInstance.save(flush:true), then that problem was gone.
I was using grails app-engine plugin 0.8.3.
And I think the source of the problem may the save closure in "app-engine-0.8.3\src\templates\jpa\scaffolding\Controller.groovy":
change:
if(${propertyName}.save())
to
if(${propertyName}.save(flush: true))
and because I didn't use jdo, I don't know whether this issue is in gae plugin jdo support.