MongoDB + Spring数据转换器与嵌入对象。

时间:2021-11-08 06:04:46

I have a write converter as

我有一个写转换器。

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;
 }
}

and my Car.java as

和我的车。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
}

and my Wheel.java as (It is not document , just a bean)

我的车轮。java as(它不是文档,只是一个bean)

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

private String wheelId;
private String name;

//getters and setters
}

I got the below error when I try to save my Car object to mongodb.

当我试图将我的汽车对象保存到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)

Please somebody help me , what is the problem ? How can I fix it ?

请大家帮帮我,有什么问题吗?我怎样才能修好它?

1 个解决方案

#1


1  

I ran into this same error. My Document looked like this:

我遇到了同样的错误。我的文件是这样的:

@Document
public class AnalyticsDailyData implements Serializable {

  private static final long serialVersionUID = -5409790992784008124L;

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

Adding an actual id field of type ObjectId made the error go away for me. I then set the metaData field to be indexed and unique.

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

@Document
public class AnalyticsDailyData implements Serializable {

  private static final long serialVersionUID = -5409790992784008124L;

  private ObjectId id;

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

I am still able to query by metaData after adding a method to my Repo:

在添加了一个方法到我的Repo之后,我仍然能够查询元数据:

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

#1


1  

I ran into this same error. My Document looked like this:

我遇到了同样的错误。我的文件是这样的:

@Document
public class AnalyticsDailyData implements Serializable {

  private static final long serialVersionUID = -5409790992784008124L;

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

Adding an actual id field of type ObjectId made the error go away for me. I then set the metaData field to be indexed and unique.

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

@Document
public class AnalyticsDailyData implements Serializable {

  private static final long serialVersionUID = -5409790992784008124L;

  private ObjectId id;

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

I am still able to query by metaData after adding a method to my Repo:

在添加了一个方法到我的Repo之后,我仍然能够查询元数据:

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