apache / apache/grails-core

Setting dynamic attrs causes problem when mongo backed domain is using class inheritance.

Open
#14,415 1 comment 0 reactions 0 assignees View on GitHub
relates-to: grails-data-mongodb
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

Domains like `Certificate` and `SpecialCert extends Certificate`. Things work well. `_class` is added to each document when doing inserts.

However, since upgrade to grails 2.5.2 and 3.0.3 version of the mongo plugin I now have a problem when adding dynamic attributes before the first save. It causes the _class not to be inserted into mongo, and this in turn causes something like `SpecialCert.get(id)` to not return the document later.

There is a workaround. Just do the .save() first then add the props and do another .save(), but it is a definite gotcha.

``` groovy
if (!specialCert.hasErrors()) {
specialCert.save()
// for some reason if we try to stamp the markerKey before the first .save() GORM mongo
// loses that what class this is and misses putting the _class into the DB causing probs later
String markerKey = ourService.getMarkerKey()
if (markerKey) specialCert['markerKey'] = markerKey
specialCert.save()
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the shown sequence with a Certificate subclass, assigning a dynamic markerKey before the first save. Inspect the Grails Mongo persistence path for new domain instances and verify whether the inserted document contains _class; done means SpecialCert.get(id) returns the saved document without requiring a preliminary save.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, mongodb
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.