Mongodb java:具有通用字段的Perist POJO类

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (145)

我有一个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

正如在这里的文档中提到的,我使用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)

我能做些什么来完成这项工作?谢谢。

提问于
用户回答回答于

有以下选项可以使其工作。

通过子类化

如果每次都使用具体的泛型类型子类,它的工作原理如下:

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

这有效,直到你避免getter / setter惯例withValue()value()方法,否则Mongo开始抱怨相同的泛型问题。

与纯粹的bson相同

我想,您也可以尝试使用与上面相同的 org.bson.codecs.pojo.PojoCodec方法来创建或从当前的mongo编解码器注册表中提取。如果你事先知道并给它真正的课程,它不应该抱怨泛型。

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动