我正在使用Spring-Batch和MongoDbWriter。
所以我们使用Spring-Data-MongoDB,当调用ItemWriter时抛出Class-Cast-Exception:
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=[{}]
我们使用spring-data-mongodb-1.7.0.RELEASE,但我认为有一个错误:
&#34; doSave&#34; MongoTemplate中的方法调用toDbObject,此方法总是返回一个BasicDBObject,除了它是一个String。因此,当我保存List时,此方法返回一个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);
}
}
}
在此之后,write() - MappingMongoConverter的方法被调用并抛出异常,因为:
if (!handledByCustomConverter && !(dbo instanceof BasicDBList)) {
typeMapper.writeType(type, dbo);
}
但由于toDbObject-Method,它不是BasicDBList。 然后调用writeInternal-Method并在那里:
if (Collection.class.isAssignableFrom(entityType)) {
writeCollectionInternal((Collection<?>) obj, ClassTypeInformation.LIST, (BasicDBList) dbo);
return;
}
这使得繁荣^^
似乎toDbObject-Method错了?这是一个错误吗?
问候
答案 0 :(得分:0)
解决方法是覆盖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);
}
}
}
}
这对我有用......但我不知道这是否有用?