如何在没有 ObjectId 的情况下将 Mongo 与 Spring Data 一起使用 [英] How to use Mongo with Spring Data without ObjectId
问题描述
我有一个现有的 mongo 数据库,其中 ids (_id) 作为普通字符串持久化.这是 Mongo DB 中的示例数据:
I have an existing mongo database in which ids (_id) are persisted as plain Strings.. This is sample data in Mongo DB:
{
"_id" : "528bb0e2e4b0442f1479f1b4",
"schoolId" : "URDLKYLFJXLWJGR193778316742298",
"surname" : "Lewis",
"admissionNumber" : "0347",
"firstName" : "Martins"
}
我有一个如下形式的 Java 对象:
I have a Java object which is of the form:
public class Student {
@Id
private String id;
private String schoolId;
private String surname;
private String admissionNumber;
private String firstName;
}
使用 Getter 和 Setter
With Getters and Setters
我也有一个仓库:
public interface StudentRepository extends MongoRepository<Student, String> {
Student findOneBySurname(String surname);
Student findOneById(String id);
Student findOneBySurnameAndFirstName(String surname, String firstName);
}
当我执行 studentRepository.findAll() 时,我能够获取数据库中的所有学生,并正确填充他们的所有字段.
When I do a studentRepository.findAll() I am able to get all the Students in the database with all their fields populated correctly.
当我执行 studentRepository.findOne("528bb0e2e4b0442f1479f1b4") 或 studentRepository.findOneById)"528bb0e2e4b0442f1479f1b4") 时,它返回 null
When I do a studentRepository.findOne("528bb0e2e4b0442f1479f1b4") or a studentRepository.findOneById)"528bb0e2e4b0442f1479f1b4") it returns null
当我调试 mongo 查询时,我可以看到它正在调用:
When I do a debug of the mongo query I can see that it is calling:
2015-11-19 16:06:32.327 DEBUG 87081 --- [ main] o.s.data.mongodb.core.MongoTemplate : findOne using query: { "id" : "528bb0e2e4b0442f1479f1b4"} fields: null for class: class com.ad.josh.domain.Student in collection: Student
2015-11-19 16:06:32.331 DEBUG 87081 --- [ main] o.s.data.mongodb.core.MongoDbUtils : Getting Mongo Database name=[joshdb]
2015-11-19 16:06:32.333 DEBUG 87081 --- [ main] o.s.data.mongodb.core.MongoTemplate : findOne using query: { "_id" : { "$oid" : "528bb0e2e4b0442f1479f1b4"}} in db.collection: josh.Student
通常我可以看到它仍在使用对象 ID 而不是纯字符串进行查询.
Typically I can see that it is still querying with Object Id and not a plain String.
我看到了以下建议:
创建一个抛出 RuntimeException 的转换器一个>
但这不起作用;它只是抛出一个 RuntimeException.
But this does not work; it just throws a RuntimeException.
关于如何使用 Spring Data 访问 Mongo 数据库的任何建议,其中 Id 已经定义为字符串(在现有数据库中).
Any suggestions on how to use Spring Data to access a Mongo database where the Ids are defined as String already (in an existing database) would be really appreciated.
推荐答案
如果您想保持字符串 ID 的原样而不是将它们转换为ObjectId 你必须覆盖 MappingMongoConverter
类中的 convertId 方法,我在这里做了一个例子:
There's a converter behind which does this conversion out of the box for you, if you'd like to keep String id's as they're and not convert them to ObjectId you have to override the convertId method in the MappingMongoConverter
class, I've made an example here:
/*
* Note that this example I've tried on spring-data-mongodb.3.0.4.RELEASE version
* and it's not guaranteed that it will work with earlier versions
* yet the approach should be similar
*/
@Component
public class CustomMappingMongoConverter extends MappingMongoConverter {
public CustomMappingMongoConverter(MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
//The constructor can differ based on the version and the dbRefResolver instance as well
super(NoOpDbRefResolver.INSTANCE, mappingContext);
}
@Override
public Object convertId(Object id, Class<?> targetType) {
if (id == null) {
return null;
} else if (ClassUtils.isAssignable(ObjectId.class, targetType) && id instanceof String && ObjectId.isValid(id.toString())) {
return id;
}
return super.convertId(id, targetType);
}
}
这篇关于如何在没有 ObjectId 的情况下将 Mongo 与 Spring Data 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!