gpt4 book ai didi

java - 如何使用 findByClassLiteral 查询构建自定义 MongoRepository

转载 作者:行者123 更新时间:2023-12-02 11:07:16 26 4
gpt4 key购买 nike

我正在尝试构建一个 MongoRepository,它允许根据类成员查找文档。

这是我的缩写数据对象:

public class Example<T extends SomeType> {

private String id;

private Class<T> dataClass;

}

这是我的存储库:

public interface ExampleRepository extends MongoRepository<Example<? extends SomeType>, String> {

<T extends SomeType> Example<T> findByDataClass(Class<T> dataClass);
}

以下是我如何保存 Example 实例的示例:

@Autowired
private ExampleRepository examples;

void someMethod() {
examples.save(new Example<>(null, SomeConcreteType.class));
}

当我尝试启动我的应用程序时,出现异常:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.lang.Class.

我不知道这是否相关,但我正在使用 Fongo 在本地构建内存数据库。

我该如何解决这个问题?

最佳答案

这是不可能实现的。如果您尝试保存,您将收到相同的错误消息。

Can't find a codec for class java.lang.Class

这是因为,Mongo 不知道这种数据类型。查看此链接以了解 Mongo 支持的数据类型。 http://mongodb.github.io/mongo-java-driver/3.0/bson/documents/

关于java - 如何使用 findByClassLiteral 查询构建自定义 MongoRepository,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50872810/

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