Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 2.2-RC1
-
Component/s: Persistence
-
Labels:None
Description
Apparently domain classes ignore a parameter named id on the constructor, which is very reasonable in most cases. However, when the id mapping is set to generator:'assigned', this can get be a bother since one has to pass all pass all parameters and then manually assign the id. An example class would be:
class CurrencyRow {
// ISO Alphabetic code
String id
String name
int numericCode
static mapping =
{ id generator:'assigned', params:[type:'string'] }static constraints =
{ id(size:1..5) name(size:2..20, unique:true) numericCode(unique:true) }}
In order to save it, one must do:
def c = new CurrencyRow(name:'one', numericCode:10)
c.id = 'CUR'
c.save()
Otherwise an exception about the id being unassigned is raised.
-
Hide
- gormTest.zip
- 12/Jun/12 8:04 PM
- 120 kB
- Michael Lawler
-
- gormTest/.classpath 0.7 kB
- gormTest/.DS_Store 6 kB
- __MACOSX/gormTest/._.DS_Store 0.1 kB
- gormTest/.project 0.5 kB
- gormTest/.../org.codehaus.groovy.eclipse.preferences.prefs 0.1 kB
- gormTest/application.properties 0.2 kB
- gormTest/.../ApplicationResources.groovy 0.1 kB
- gormTest/grails-app/.../BootStrap.groovy 0.1 kB
- gormTest/grails-app/.../BuildConfig.groovy 2 kB
- gormTest/grails-app/conf/Config.groovy 4 kB
- gormTest/grails-app/.../DataSource.groovy 1 kB
- gormTest/grails-app/.../resources.groovy 0.0 kB
- gormTest/grails-app/.../UrlMappings.groovy 0.2 kB
- gormTest/grails-app/.../CurrencyRow.groovy 0.3 kB
- __MACOSX/gormTest/.../._CurrencyRow.groovy 0.2 kB
- gormTest/grails-app/.../messages.properties 3 kB
- gormTest/.../messages_cs_CZ.properties 3 kB
- gormTest/.../messages_da.properties 3 kB
- gormTest/.../messages_de.properties 4 kB
- gormTest/.../messages_es.properties 3 kB
- gormTest/.../messages_fr.properties 2 kB
- gormTest/.../messages_it.properties 2 kB
- gormTest/.../messages_ja.properties 4 kB
- gormTest/.../messages_nl.properties 3 kB
- gormTest/.../messages_pt_BR.properties 3 kB
- gormTest/.../messages_pt_PT.properties 3 kB
- gormTest/.../messages_ru.properties 4 kB
- gormTest/.../messages_sv.properties 3 kB
- gormTest/.../messages_th.properties 6 kB
- gormTest/.../messages_zh_CN.properties 2 kB
-
Hide
- grails1984.zip
- 21/Jun/12 4:18 PM
- 115 kB
- Jeff Brown
-
- grails1984/.classpath 0.7 kB
- grails1984/.gitignore 0.0 kB
- grails1984/.project 0.5 kB
- grails1984/.../org.codehaus.groovy.eclipse.preferences.prefs 0.1 kB
- grails1984/application.properties 0.1 kB
- grails1984/.../ApplicationResources.groovy 0.1 kB
- grails1984/grails-app/.../BootStrap.groovy 0.3 kB
- grails1984/grails-app/.../BuildConfig.groovy 2 kB
- grails1984/grails-app/conf/Config.groovy 4 kB
- grails1984/grails-app/.../DataSource.groovy 1 kB
- grails1984/grails-app/.../UrlMappings.groovy 0.2 kB
- grails1984/grails-app/.../resources.groovy 0.0 kB
- grails1984/.../CurrencyRowController.groovy 0.1 kB
- grails1984/grails-app/.../CurrencyRow.groovy 0.3 kB
- grails1984/.../messages.properties 3 kB
- grails1984/.../messages_cs_CZ.properties 3 kB
- grails1984/.../messages_da.properties 3 kB
- grails1984/.../messages_de.properties 4 kB
- grails1984/.../messages_es.properties 3 kB
- grails1984/.../messages_fr.properties 2 kB
- grails1984/.../messages_it.properties 3 kB
- grails1984/.../messages_ja.properties 4 kB
- grails1984/.../messages_nl.properties 3 kB
- grails1984/.../messages_pl.properties 4 kB
- grails1984/.../messages_pt_BR.properties 3 kB
- grails1984/.../messages_pt_PT.properties 3 kB
- grails1984/.../messages_ru.properties 4 kB
- grails1984/.../messages_sv.properties 3 kB
- grails1984/.../messages_th.properties 6 kB
- grails1984/.../messages_zh_CN.properties 2 kB
Issue Links
- relates to
-
GRAILS-8422
Allow the specification of ids in findOrSaveWhere
-
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
I think this is a clone of
http://jira.codehaus.org/browse/GRAILS-3431
or vice versa.
There is no scope / time to resolve these remaining lower priority issues for 1.2 so moving to 1.3
for 1.2 final only issues considered blocking will now be fixed
It seemed to me that this worked for Grails 2.0.0 and 2.0.1 but does not work for Grails 2.0.3 any more. Can anybody confirm that?
Dominik, it could be a problem related to GRAILS-8972 . Try adding "bindable:true" to id's constraint definition.
This is a minimal grails 2.0.3 example as per the description, with bindable:true on the String id. However id initialisation still does not work.
I tried it with 2.0.4 and it was not successful either.
Any help appreciated as this is causing us test harnesses pain for a framework with a lot of generated:assigned ids.
Probably related to the data binding changes. Jeff - please take a look
Just to add more information, I've tried again with 2.1.0.RC3 and no luck.
Attaching grails1984.zip which contains a 2.1.0.RC3 project. Bootstrap is creating instances of CurrencyRow and assigning an id as a constructor argument successfully.
Are there scenarios which do not work in 2.1.0.RC3?
It doesn't work when used within a controller or service. E.g. try adding a CurrencyRow using the CurrencyRowController of your example or add a method to the controller:
def testme() {
def v = new CurrencyRow(id: "test", numericCode: 100)
render "id: ${v.id}"
}
I see now why the behavior is different in a controller than it is in Bootstrap.
Will get this sorted out asap.
Thanks for the info.
I've written a small article with a temporary workaround for this bug: http://www.redcube.de/assigning-id-domain-objects-grails-via-constructor/
Try this way
def v = new CurrencyRow(numericCode: 100)
v.id="test"
or
def v = new CurrencyRow()
v.id="test"
v.numericCode=100
This is still an issue in 2.2.0. The only way to get the domain object created/stored successfully is to assign id separately from the constructor.
It works when you add the (undocumented?) constraint "bindable: true" to the id field:
class MyDomain {
static constraints = {
// allow binding of "id" attribute (e.g. in constructor or url parameters)
id bindable: true
}
}
FYI...
The bindable constraint is documented at http://grails.org/doc/latest/ref/Constraints/bindable.html and more details about secure data binding are noted at http://grails.org/doc/latest/guide/single.html#dataBinding.
Moving non-critical issues that aren't going to make it into 1.1 to 1.2