Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 2.0-RC2, 2.0-RC3, 2.0.1
-
Fix Version/s: 2.2-RC3
-
Component/s: Persistence
-
Labels:
-
Environment:Java 6.0.29, Win 7
Description
Trying to achieve the following:
I have:
class Entry {
String value
static mapping = {
id generator:'assigned'
}
...
}
Now I'd like to do the following:
Entry entry = Entry.findOrSaveWhere(id:key, value:value);
But this raises:
org.springframework.orm.hibernate3.HibernateSystemException: ids for this class must be manually assigned before calling save()
This is probably due to the fact that "id generator:'assigned'" for
the Entry class but it is a bit strange that the id cannot be specified in the
constructor (not sure if this holds in general). Please improve this.
If it worked, it would really improve readability and potentially even
avoid an additional save() which is required when doing:
Entry entry = Entry.findOrCreateWhere(id:key, value:value) entry.id = key entry.save()
-
Hide
- grails8422.zip
- 06/Dec/11 11:21 PM
- 128 kB
- Fat Zopilot
-
- grails8422/.classpath 5 kB
- grails8422/.gitignore 0.0 kB
- grails8422/.project 1 kB
- grails8422/.../com.google.gwt.eclipse.core.prefs 0.1 kB
- grails8422/.../com.springsource.sts.grails.core.prefs 0.2 kB
- grails8422/.../org.codehaus.groovy.eclipse.preferences.prefs 0.1 kB
- grails8422/.../org.eclipse.core.resources.prefs 1.0 kB
- grails8422/.../org.eclipse.wst.common.project.facet.core.xml 0.1 kB
- grails8422/application.properties 0.3 kB
- grails8422/.../ApplicationResources.groovy 0.1 kB
- grails8422/grails-app/.../BootStrap.groovy 0.1 kB
- grails8422/grails-app/.../BuildConfig.groovy 2 kB
- grails8422/grails-app/conf/Config.groovy 4 kB
- grails8422/grails-app/.../DataSource.groovy 1 kB
- grails8422/grails-app/.../resources.groovy 0.0 kB
- grails8422/grails-app/.../UrlMappings.groovy 0.2 kB
- grails8422/.../WebXmlConfig.groovy 2 kB
- grails8422/.../EntryController.groovy 4 kB
- grails8422/grails-app/.../demo/Entry.groovy 0.2 kB
- grails8422/.../messages.properties 3 kB
- grails8422/.../messages_cs_CZ.properties 3 kB
- grails8422/.../messages_da.properties 3 kB
- grails8422/.../messages_de.properties 4 kB
- grails8422/.../messages_es.properties 3 kB
- grails8422/.../messages_fr.properties 2 kB
- grails8422/.../messages_it.properties 2 kB
- grails8422/.../messages_ja.properties 4 kB
- grails8422/.../messages_nl.properties 3 kB
- grails8422/.../messages_pt_BR.properties 3 kB
- grails8422/.../messages_pt_PT.properties 3 kB
-
Hide
- grails8422.zip
- 02/Dec/11 12:20 PM
- 118 kB
- Jeff Brown
-
- grails8422/.classpath 0.7 kB
- grails8422/.gitignore 0.0 kB
- grails8422/.project 0.5 kB
- grails8422/.../org.codehaus.groovy.eclipse.preferences.prefs 0.1 kB
- grails8422/application.properties 0.2 kB
- grails8422/.../ApplicationResources.groovy 0.1 kB
- grails8422/grails-app/.../BootStrap.groovy 0.2 kB
- grails8422/grails-app/.../BuildConfig.groovy 2 kB
- grails8422/grails-app/conf/Config.groovy 4 kB
- grails8422/grails-app/.../DataSource.groovy 1 kB
- grails8422/grails-app/.../UrlMappings.groovy 0.2 kB
- grails8422/grails-app/.../resources.groovy 0.0 kB
- grails8422/.../EntryController.groovy 4 kB
- grails8422/grails-app/.../demo/Entry.groovy 0.1 kB
- grails8422/.../messages.properties 3 kB
- grails8422/.../messages_cs_CZ.properties 3 kB
- grails8422/.../messages_da.properties 3 kB
- grails8422/.../messages_de.properties 4 kB
- grails8422/.../messages_es.properties 3 kB
- grails8422/.../messages_fr.properties 2 kB
- grails8422/.../messages_it.properties 2 kB
- grails8422/.../messages_ja.properties 4 kB
- grails8422/.../messages_nl.properties 3 kB
- grails8422/.../messages_pt_BR.properties 3 kB
- grails8422/.../messages_pt_PT.properties 3 kB
- grails8422/.../messages_ru.properties 4 kB
- grails8422/.../messages_sv.properties 3 kB
- grails8422/.../messages_th.properties 6 kB
- grails8422/.../messages_zh_CN.properties 2 kB
- grails8422/grails-app/.../entry/_form.gsp 0.3 kB
Issue Links
- is related to
-
GRAILS-1984
id initialization on constructor when generator is assigned
-
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
Fat,
One scenario that might lead to a problem is if the value of your key variable is null. That would probably lead to the exception you mentioned.
Jeff,
Your project works without any problems. Meanwhile, I found the following:
If a domain object constructor is invoked with an id inside a service methods that is called via RPC (using the GWT plugin, don't know whether other remote services are affected as well), the id stays null and must be set via setId();
That's why the findOrSaveWhere method fails there as well.
All these services extend RemoteServiceServlet. Other services don't seem to be affected. Do you have an idea why this is happening?
Fat,
To help be clear about what situations are problematic, can you attach the simplest app that demonstrates the problem you are seeing?
Thanks for the help.
Hm, I fear it won't be that simple as it involves remoting. Have you seen similar tests/apps already in the test suites (testing some remoting related stuff) that could be extended?
OK, just updated your project to show the issue. It uses GWT for remoting. Best way to get it running is a grails war first (to compile the GWT JS), then simply run it and open the index page. This will trigger a remote call to SomeService where a new Entry with assigned id is generated (which fails). Suggest to consider it a bug now...
Fat,
I don't see any calls to findOrSaveWhere in the project you attached.
There is a service which looks like this:
package com.demo class SomeService { static expose = [ 'gwt:com.demo.GWTApp' ] void serviceMethod() { Entry entry = new Entry(value: 'whatever', id: 16l); entry.save(); } }
Is that the problematic code?
This bug seems to be fixed by GRAILS-1984. (Grails 2.2-RC1)
It is sufficient to mark the id as "bindable:true" to allow assignment in constructor / findOrSaveWhere / findOrCreateWhere.
Example:
class Entry {
String value
static constraints = {
id bindable: true
}
static mapping = {
id generator: 'assigned'
}
}
Fat,
Can you take a look at the attached project and let me know if this works for you. This is an RC3 app but I see the same behavior in RC2.
Note that BootStrap is invoking findOrSaveWhere twice.
Let me know if this works for you.
Thanks for the help.