You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Upgrading an application from Grails 3.2.8 to Grails 4.0.5 (GORM 7.0.x) exposed a problem with domain classes mapped as a hierarchy: after a successful .save() on a domain instance, retrieving it back via .get() would result in a partially filled instance. A number of properties would not have their values loaded, and be left at null values. No exception or errors would be reported by GORM.
The base domain class in the application has "_class" persistent property defined for receiving discriminator values.
An inspection of the runtime GORM behavior shows that BsonPersistentEntityCodec writes out the discriminator "_class" property, and then does it again as a part of the persistent property set - see its encode method. The resulting class representation in persistent storage has two "_class" properties. This is possible because Mongo BSON allows storing duplicate properties.
On deserializing the domain class the discriminator-named property gets encountered twice. Processing the second occurence discards the partially resolved domain instance and creates a new instance to fill with property values - see decode method.
Removing the _class property definition from the domain class has the effect of it getting added/persisted by the codec as a dynamic attribute. The resulting BSON representation of the class still has two _class properties.
A workaround is to keep the _class property in the domain class, but define it as transient. This results in only the discriminator property getting serialized into BSON, with deserialization not getting re-started midway, and the domain instance getting loaded fully.
The text was updated successfully, but these errors were encountered:
Upgrading an application from Grails 3.2.8 to Grails 4.0.5 (GORM 7.0.x) exposed a problem with domain classes mapped as a hierarchy: after a successful .save() on a domain instance, retrieving it back via .get() would result in a partially filled instance. A number of properties would not have their values loaded, and be left at null values. No exception or errors would be reported by GORM.
The base domain class in the application has "_class" persistent property defined for receiving discriminator values.
An inspection of the runtime GORM behavior shows that BsonPersistentEntityCodec writes out the discriminator "_class" property, and then does it again as a part of the persistent property set - see its encode method. The resulting class representation in persistent storage has two "_class" properties. This is possible because Mongo BSON allows storing duplicate properties.
On deserializing the domain class the discriminator-named property gets encountered twice. Processing the second occurence discards the partially resolved domain instance and creates a new instance to fill with property values - see decode method.
Removing the _class property definition from the domain class has the effect of it getting added/persisted by the codec as a dynamic attribute. The resulting BSON representation of the class still has two _class properties.
A workaround is to keep the _class property in the domain class, but define it as transient. This results in only the discriminator property getting serialized into BSON, with deserialization not getting re-started midway, and the domain instance getting loaded fully.
The text was updated successfully, but these errors were encountered: