gpt4 book ai didi

java - 带有嵌入对象的 MongoDB + Spring 数据转换器

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:00 25 4
gpt4 key购买 nike

我有一个写入转换器

public class CarConverter implements Converter<Car, DBObject> {

@Override
public final DBObject convert(final Car car) {
DBObject dbo = new BasicDBObject();
dbo.put("_id", car.getId());
// below line produces error
dbo.put("wheels", car.getWheels());
return dbo;
}
}

我的Car.java

@Document(collection = "car")
public class Car implements Serializable {
private static final long serialVersionUID = 6121244806685144430L;

@Id private String id;
private List<Wheel> wheels;

// getters and setters
}

我的Wheel.java为(它不是文档,只是一个bean)

public class Wheel implements Serializable {
private static final long serialVersionUID = 6121244806685144430L;

private String wheelId;
private String name;

//getters and setters
}

当我尝试将我的Car对象保存到 mongodb 时,出现以下错误。

SLF4J: Failed toString() invocation on an object of type [com.mongodb.BasicDBObject]
java.lang.RuntimeException: json can't serialize type : class com.mypackage.Wheel
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:77)
at com.mongodb.util.JSONSerializers$IterableSerializer.serialize(JSONSerializers.java:290)
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:317)
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
at com.mongodb.util.JSON.serialize(JSON.java:55)
at com.mongodb.util.JSON.serialize(JSON.java:40)
at com.mongodb.BasicDBObject.toString(BasicDBObject.java:83)
at org.slf4j.helpers.MessageFormatter.safeObjectAppend(MessageFormatter.java:276)
at org.slf4j.helpers.MessageFormatter.deeplyAppendParameter(MessageFormatter.java:248)
at org.slf4j.helpers.MessageFormatter.arrayFormat(MessageFormatter.java:206)
at org.slf4j.helpers.MessageFormatter.format(MessageFormatter.java:148)
at org.slf4j.impl.Log4jLoggerAdapter.info(Log4jLoggerAdapter.java:341)
INFO : org.springframework.data.mongodb.core.mapping.event.LoggingEventListener - onBeforeSave:
com.mypackage.Car@1e4ec58, [FAILED toString()]
java.lang.IllegalArgumentException: can't serialize class com.mypackage.Wheel
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:284)
at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:309)
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:248)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:185)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:131)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:33)
at com.mongodb.OutMessage.putObject(OutMessage.java:289)
at com.mongodb.OutMessage.writeUpdate(OutMessage.java:180)
at com.mongodb.OutMessage.update(OutMessage.java:60)
at com.mongodb.DBCollectionImpl.update(DBCollectionImpl.java:275)
at com.mongodb.DBCollection.update(DBCollection.java:191)
at com.mongodb.DBCollection.save(DBCollection.java:975)
at com.mongodb.DBCollection.save(DBCollection.java:934)

请有人帮助我,有什么问题吗?我该如何修复它?

最佳答案

我遇到了同样的错误。我的文档如下所示:

@Document
public class AnalyticsDailyData implements Serializable {

private static final long serialVersionUID = -5409790992784008124L;

@Id
@NotNull
@Valid
private AnalyticsMetaData metaData;
...

添加 ObjectId 类型的实际 id 字段使错误消失。然后,我将元数据字段设置为索引且唯一。

@Document
public class AnalyticsDailyData implements Serializable {

private static final long serialVersionUID = -5409790992784008124L;

private ObjectId id;

@Indexed(unique = true)
@NotNull
@Valid
private AnalyticsMetaData metaData;
...

在我的存储库中添加方法后,我仍然可以通过元数据进行查询:

public interface AnalyticsDailyDataRepo extends PagingAndSortingRepository<AnalyticsDailyData,   ObjectId> {
AnalyticsDailyData findOneByMetaData(AnalyticsMetaData meta);
}

关于java - 带有嵌入对象的 MongoDB + Spring 数据转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com