Spring Data MongoDB - class exception while persisting list

I am using Spring-Batch with MongoDbWriter.

So, we are using Spring-Data-MongoDB and when the ItemWriter is called Class-Cast-Exception, it is thrown:

 10:40:13.795 [jobLauncherTaskExecutor-1] DEBUG o.s.b.c.r.dao.JdbcJobExecutionDao - Truncating long message before update of JobExecution: JobExecution: id=0, version=1, startTime=Wed Jun 17 10:40:01 CEST 2015, endTime=Wed Jun 17 10:40:13 CEST 2015, lastUpdated=Wed Jun 17 10:40:13 CEST 2015, status=FAILED, exitStatus=exitCode=FAILED;exitDescription=java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.mongodb.BasicDBList
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:384)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:353)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:78)
    at org.springframework.data.mongodb.core.MongoTemplate.toDbObject(MongoTemplate.java:809)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:962)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:911)
    at org.springframework.batch.item.data.MongoItemWriter.doWrite(MongoItemWriter.java:128)
    at org.springframework.batch.item.data.MongoItemWriter$1.beforeCommit(MongoItemWriter.java:156)
    at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:95)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:928)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:740)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:150)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:386)
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
, job=[JobInstance: id=0, version=0, Job=[NBO.READER.]], jobParameters=[{}]

      

We are using Spring-data-mongodb-1.7.0.RELEASE, but I think there is a bug:

MongoTemplate's doSave method calls toDbObject and this method always returns a BasicDBObject, except it's a string. So when I save the list, this method returns a BasicDBObject ...

    private <T> DBObject toDbObject(T objectToSave, MongoWriter<T> writer) {

        if (!(objectToSave instanceof String)) {
            DBObject dbDoc = new BasicDBObject();
            writer.write(objectToSave, dbDoc);
            return dbDoc;
        } else {
            try {
                return (DBObject) JSON.parse((String) objectToSave);
            } catch (JSONParseException e) {
                throw new MappingException("Could not parse given String to save into a JSON document!", e);
            }
        }
    }

      

After that, the write () method is called - MappingMongoConverter and throws an exception, because:

    if (!handledByCustomConverter && !(dbo instanceof BasicDBList)) {
                typeMapper.writeType(type, dbo);
            }

      

but it is not BasicDBList, because of the toDbObject-Method. Then the writeInternal-Method is called and there:

if (Collection.class.isAssignableFrom(entityType)) {
            writeCollectionInternal((Collection<?>) obj, ClassTypeInformation.LIST, (BasicDBList) dbo);
            return;
        }

      

It makes an arrow ^^

Seems like toDbObject-Method is wrong? This is mistake?

Hello

+3


source to share


1 answer


A workaround is to override the MongoTemplate class:

public class MongoHack extends MongoTemplate
{

    public MongoHack(MongoDbFactory mongoDbFactory)
    {
        super(mongoDbFactory);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T> writer)
    {

        this.assertUpdateableIdIfNotSet(objectToSave);

        maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave));

        DBObject dbDoc = this.toDbObject(objectToSave, writer);

        maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc));
        Object id = saveDBObject(collectionName, dbDoc, objectToSave.getClass());

        populateIdIfNecessary(objectToSave, id);
        maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc));
    }

    private void assertUpdateableIdIfNotSet(Object entity)
    {

        MongoPersistentEntity<?> persistentEntity = super.getConverter().getMappingContext()
                .getPersistentEntity(entity.getClass());
        MongoPersistentProperty idProperty = persistentEntity == null ? null : persistentEntity.getIdProperty();

        if (idProperty == null || persistentEntity == null) {
            return;
        }

        Object idValue = persistentEntity.getPropertyAccessor(entity).getProperty(idProperty);

        if (idValue == null && !MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(idProperty.getType())) {
            throw new InvalidDataAccessApiUsageException(String.format(
                    "Cannot autogenerate id of type %s for entity of type %s!", idProperty.getType().getName(), entity
                            .getClass().getName()));
        }
    }

    private <T> DBObject toDbObject(T objectToSave, MongoWriter<T> writer)
    {

        if (Collection.class.isAssignableFrom(objectToSave.getClass())) {
            DBObject dbDoc = new BasicDBObject();
            Collection<T> objects = (Collection<T>) objectToSave;
            Iterator<T> iterator = objects.iterator();
            while(iterator.hasNext()) {
                writer.write(iterator.next(), dbDoc);
            }
            return dbDoc;
        }

        else if (!(objectToSave instanceof String)) {
            DBObject dbDoc = new BasicDBObject();
            writer.write(objectToSave, dbDoc);
            return dbDoc;

        }

        else {
            try {
                return (DBObject) JSON.parse((String) objectToSave);
            }
            catch (JSONParseException e) {
                throw new MappingException("Could not parse given String to save into a JSON document!", e);
            }
        }
    }

}

      



This works for me ... But I don't know if this is a useful usecase?

0


source







All Articles