gpt4 book ai didi

java - MongoDB java : Perist POJO class with generic field

转载 作者:行者123 更新时间:2023-12-01 14:33:49 24 4
gpt4 key购买 nike

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

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

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 + Jackson

为避免子类化,您可以尝试将任意对象存储为 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 编解码器注册表中提取它。如果你提前知道并给它真正的类,它不应该提示泛型。

关于java - MongoDB java : Perist POJO class with generic field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52197137/

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