Announcement Announcement Module
Collapse
No announcement yet.
MongoDB Abstract Type Mapping excludes _class when Pushing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB Abstract Type Mapping excludes _class when Pushing

    I am have the following class hierarchy.
    Code:
    public class Sensor {
      List<Event> events;
    }
    
    public abstract class Event { ... }
    
    public class PressureEvent extends Event { ... }
    When I save a Sensor instance with an event, the _class property is added.
    Code:
    mongoTemplate.save(sensor, Sensor.class);
    
    { "_class" : "com.acme.Sensor",
       "events" : [
    		{
    			 "_class" : "com.acme.PressureEvent",
                             ...
    		}
       ]
    }
    When I push an event onto the Sensor instance's event array the _class property is not added.
    Code:
    mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(new ObjectId(sensor.getId()))), new Update().push("events", event), Sensor.class);
    
    { "_class" : "com.acme.Sensor",
       "events" : [
    		{
    			 ...
    		}
       ]
    }
    After an Event instance without a _class field is pushed onto the events array the sensor instance cannot be loaded due to the mapper not knowing the type of class.

    Code:
    org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [com.acme.Event]: Is it an abstract class?; nested exception is java.lang.InstantiationException
    Is this a bug or the intended behavior? Am I pushing onto the array improperly?

    I am using spring-data-mongodb-1.0.1.RELEASE.

  • #2
    Did you find a resolution to this? I'm running into the same issue on 1.0.2 RELEASE.

    Comment

    Working...
    X