Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic Attribute is stored first time but not followings in grails application with mongodb
    primarykey
    data
    text
    <p>I'm trying to add one dynamic attribute to my Domain Class and I'm experimenting an extrange issue. When the database is empty, the first instance is saved with the property correctly set, but subsequent calls store the instance without that property. Any ideas?</p> <p>Also note that I've set nullable:true by default in Config.groovy to allow adding new properties just where I need them</p> <p>This is my domain</p> <pre><code>class UserBehaviour extends UserActivity{ Behaviour behaviour Site site int points static belongsTo = [behaviour:Behaviour,site:Site] } </code></pre> <p>This the code for saving with some println</p> <pre><code>UserBehaviour userBehaviour = new UserBehaviour(user:user,behaviour:behaviour,site:site,points:behaviour.points,date:new Date()) metadata.each{key,value-&gt; println "metadata${key}" userBehaviour["metadata${key}"] = value println userBehaviour["metadata${key}"] } println metadata if (!userBehaviour.save()){ throw new InvalidRequestException("invalid") } else { println "before--&gt;"+UserBehaviour.countByMetadatalanguage("java") //do some things.... println "after--&gt;"+UserBehaviour.countByMetadatalanguage("java") } </code></pre> <p>And this is the output of calling it twice</p> <pre><code>metadatalanguage java [language:java] before--&gt;0 after--&gt;1 metadatalanguage null [language:java] before--&gt;1 after--&gt;1 </code></pre> <p><strong>UPDATE 1</strong> These are the stored documents, as you can see they're almost equal except for the _id and the date (both of them should have the metadatalanguage property...)</p> <pre><code>{ _id: 51d50ff5c6c3cf71b9fc2a38 _class: UserBehaviour user: 519f0e14c6c3da4f668c1d3c behaviour: 51d410a2c6c3934c7207df6b metadatalanguage: java points: 50 date: Thu Jul 04 08:02:29 CEST 2013 site: 519f0e15c6c3da4f668c1d75 version: 3 } { _id: 51d50ff7c6c3cf71b9fc2a3a _class: UserBehaviour user: 519f0e14c6c3da4f668c1d3c behaviour: 51d410a2c6c3934c7207df6b points: 50, date: Thu Jul 04 08:02:31 CEST 2013 site: 519f0e15c6c3da4f668c1d75 version: 0 } </code></pre> <p>One strange thing I've just seen is the version number. I haven't modified any of these instances, so version should be 0 right?</p> <p><strong>UPDATE 2</strong> I've created an small integration test that also fails when checking userBehaviour2.metadatalanguage.</p> <pre><code>void testAddDynamicProperty(){ setup: def metadata = ["language":"java"] Behaviour behaviour = Behaviour.list([max:1]).first() User user = User.list([max:1]).first() when: UserBehaviour userBehaviour = new UserBehaviour(user:user,behaviour:behaviour,site:site,points:behaviour.points,date:new Date()) metadata.each{key,value-&gt; userBehaviour."metadata${key}" = value } UserBehaviour userBehaviour2 = new UserBehaviour(user:user,behaviour:behaviour,site:site,points:behaviour.points,date:new Date()) metadata.each{key,value-&gt; userBehaviour2."metadata${key}" = value } boolean valid1 = userBehaviour.save() boolean valid2 = userBehaviour2.save() then: valid1 == true valid2 == true userBehaviour.metadatalanguage == "java" userBehaviour2.metadatalanguage == "java" } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload