I have an object like the following:
class User {
static mapWith="mongo"
static embedded = [ 'profiles' ]
String email
List<Profile> profiles;
}
interface Profile {
}
class Profile1 implements Profile {
}
class Profile2 implements Profile {
}
If I add the concrete classes Profile1 or Profile2 to the User object and save it to the database it throws an exception when reading that object back out of MongoDB. I don't see any information being saved to the DB to identify which type of object it should instantiate in this situation. And there is exactly ZERO documentation about how this case is handled. Other frameworks have mechanisms for handling this so either Grails MongoDB is woefully broken, or this is just undocumented (again). So how do I fix this?
The exception is the following:
| Error 2013-06-12 18:48:00,390 [http-bio-8080-exec-5] ERROR errors.GrailsExceptionResolver - InstantiationException occurred when processing request: [POST] /mpa/user/authenticate -parameters:
email: carhubb@gmail.com
password: ***
com.mycompany.security.Profile. Stacktrace follows:
Message: com.mycompany.security.Profile
Line | Method
->> 342 | newInstance0 in java.lang.Class
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 310 | newInstance in ''
The actual answer is that grails seems to handle classes but not interfaces which is truly bizarre because if you handle polymorphism for classes it's trivial to handle it for interfaces because you can handle it the same way. But if you use classes for all reference types it will add a special '_class' property to mongodb and it will use that to instantiate the object of the actual reference the object pointed to when it was saved. Now how hard was that to explain in one paragraph rather than trolling through pages of source code and unit tests?