Mongodb Java:具有泛型字段的Perist POJO类

时间:2018-09-06 05:34:36

标签: java mongodb mongodb-java

我有一个看起来像这样的POJO类-

public class CacheEntity<V> {
  private String cacheId;
  private V value;

  public static final String ID_KEY = "cacheId";

  public CacheEntity() {
  }

  public CacheEntity(String cache_id, V value) {
    this.cacheId = cache_id;
    this.value = value;
  }

  public V getValue() {
    return value;
  }

  public void setValue(V value) {
    this.value = value;
  }

  public String getCacheId() {
    return cacheId;
  }

  public void setCacheId(String cacheId) {
    this.cacheId = cacheId;
  }
}

我正在使用官方驱动程序mongodb-driver-sync版本3.8.1

如文档here中所述,我正在将CodecRegistry与自动PojoCodecProvider一起使用。尝试保留此实体时出现以下错误。

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: CacheEntity contains generic types that have not been specialised.
Top level classes with generic types are not supported by the PojoCodec.
    at org.bson.codecs.pojo.PojoCodecImpl.encode(PojoCodecImpl.java:93)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
    at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:398)
    at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:377)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
    at com.mongodb.internal.connection.BsonWriterHelper.writeDocument(BsonWriterHelper.java:75)
    at com.mongodb.internal.connection.BsonWriterHelper.writePayload(BsonWriterHelper.java:59)
    at com.mongodb.internal.connection.CommandMessage.encodeMessageBodyWithMetadata(CommandMessage.java:143)
    at com.mongodb.internal.connection.RequestMessage.encode(RequestMessage.java:138)
    at com.mongodb.internal.connection.CommandMessage.encode(CommandMessage.java:57)
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:244)
    at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
    at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444)
    at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
    at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200)
    at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
    at com.mongodb.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:418)
    at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:256)
    at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:67)
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:200)
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:191)
    at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:419)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:191)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:67)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:193)
    at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:960)
    at com.mongodb.client.internal.MongoCollectionImpl.executeReplaceOne(MongoCollectionImpl.java:602)
    at com.mongodb.client.internal.MongoCollectionImpl.replaceOne(MongoCollectionImpl.java:578)
    at com.mongodb.client.internal.MongoCollectionImpl.replaceOne(MongoCollectionImpl.java:567)

我该怎么做才能使它起作用?谢谢。

1 个答案:

答案 0 :(得分:0)

有以下几种方法可以使其正常工作。

通过子类化

如果您每次都使用具体的泛型类型进行子类化,那么它将起作用:

public class MyClassCacheEntity extends CacheEntity<MyClass> {}

使用BsonDocument +杰克逊

为避免子类化,您可以尝试将任意对象存储为BsonDocument -s并使用Jackson对其进行序列化/反序列化:

public class CacheEntity<V> {
  private String cacheId;
  private BsonDocument rawValue;

  // [...]

  // sets the value
  public void withValue(T o) {
    this.rawValue = BsonDocument.parse(new ObjectMapper().writeValueAsString(o))
  }

  // recovers the value
  public T value(Class<T> clazz) {
    return new ObjectMapper().readValue(this.rawValue.toJson(), clazz);
  }
}

这一直有效,直到您避免为withValue()value()方法使用getters / setters约定,否则Mongo开始抱怨相同的泛型问题。

与纯bson相同

我认为,您也可以尝试使用org.bson.codecs.pojo.PojoCodec来完成上述操作,您可以从当前的mongo编解码器注册表中创建或提取。如果您事先知道并提供了真实的类,那么它就不会抱怨泛型。