apache / apache/grails-core

java.lang.InstantiationException when using inheritance and abstract base class and embedded object

Open
#14,394 0 comments 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

### Task List

- [x] Steps to reproduce provided
- [x] Stacktrace (if present) provided
- [x] Example that reproduces the problem uploaded to Github
- [x] Full description of the issue provided (see below)

When using Inheritance from an abstract base class as an embedded object, the `list` criteria query throws `java.lang.InstantiationException`.

### Steps to Reproduce

1. Create a Grails 4 application.
2. Remove hibernate dependencies and add the Grails MongoDB plugin.
3. Create an abstract base domain class `Address` as:
```
abstract class Address {
String street
}
```
4. Create another domain class `HomeAddress.groovy` which implements `Address` as:
```
class HomeAddress extends Address {

String type = "home"
}
```
5. Create a domain class `Patient.groovy` which as embedded address as:
```
class Patient {

Address address

static embedded = ['address']
}
```
6. Try to access list of all patient using following query:
```
Patient.list()
```

### Expected Behaviour

The query `Patient.list()` should return all the patients with embedded address information without any error.
### Actual Behaviour

The following exception is thrown:
```
Caused by: java.lang.reflect.UndeclaredThrowableException
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:319)
at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:40)
at org.grails.datastore.gorm.GormStaticApi.list(GormStaticApi.groovy:607)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.list(GormEntity.groovy:682)
... 1 more
Caused by: java.lang.InstantiationException
at org.grails.datastore.bson.codecs.BsonPersistentEntityCodec.decode(BsonPersistentEntityCodec.groovy:76)
at org.grails.datastore.bson.codecs.decoders.EmbeddedDecoder.decode(EmbeddedDecoder.groovy:27)
at org.grails.datastore.bson.codecs.BsonPersistentEntityCodec.decode(BsonPersistentEntityCodec.groovy:121)
at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47)
at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at com.mongodb.internal.connection.ReplyMessage.(ReplyMessage.java:48)
at com.mongodb.internal.connection.InternalStreamConnection.getCommandResult(InternalStreamConnection.java:393)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444)
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200)
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:123)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:242)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:233)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:136)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:701)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:695)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:462)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:695)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:83)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179)
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132)
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86)
at org.grails.datastore.mapping.mongo.query.MongoQuery.executeQuery(MongoQuery.java:527)
at org.grails.datastore.mapping.mongo.query.MongoQuery.executeQuery(MongoQuery.java:464)
at org.grails.datastore.mapping.query.Query.doList(Query.java:589)
at org.grails.datastore.mapping.query.Query.list(Query.java:568)
at org.grails.datastore.gorm.GormStaticApi.list_closure20(GormStaticApi.groovy:608)
at groovy.lang.Closure.call(Closure.java:405)
... 5 more

```

### Environment Information

- **Operating System**: macOS
- **Grails Version:** 4.0.3
- **JDK Version:** 1.8.hs-adpt
- **Container Version (If Applicable):** MongoDB 3 Docker Image

### Example Application

- https://github.com/puneetbehl/mongodbdemo/tree/abstractEmbeddedMongo

Contributor guide

Open the contributing guide

Research direction

Start by running the linked example application and reproducing Patient.list() with the embedded abstract Address hierarchy. Then inspect org.grails.datastore.bson.codecs.BsonPersistentEntityCodec.groovy and EmbeddedDecoder.groovy from the stack trace; done means the query returns patients with embedded address data without InstantiationException.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, mongodb
Domain
backend, 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.