我有自己的包含复杂类层次结构的pojo(具有继承支持的嵌入式文档)。我已经在不使用@TypeAlias的情况下使用ConfigurableTypeInformationMapper实现了对继承映射的支持。我也希望我的pojo不依赖于org.springframework。data:spring-data-mongodb的框架,但添加OptimisticLocking功能需要用@Version注释字段。是否有一种方法来配置版本属性,而不使用@Version注释在Spring Data MongoDB?
对于那些对如何在pojo中不依赖@Version注释感兴趣的人,请查看MyCachingMongoPersistentProperty类。
class MyMongoMappingContext : MongoMappingContext() {
private var fieldNamingStrategy: FieldNamingStrategy? = null
override fun setFieldNamingStrategy(fieldNamingStrategy: FieldNamingStrategy?) {
if (fieldNamingStrategy == null) {
this.fieldNamingStrategy = DEFAULT_NAMING_STRATEGY
super.setFieldNamingStrategy(DEFAULT_NAMING_STRATEGY)
} else {
this.fieldNamingStrategy = fieldNamingStrategy
super.setFieldNamingStrategy(fieldNamingStrategy)
}
}
override fun createPersistentProperty(
property: Property,
owner: MongoPersistentEntity<*>,
simpleTypeHolder: SimpleTypeHolder
): MongoPersistentProperty {
return MyCachingMongoPersistentProperty(
property, owner, simpleTypeHolder, fieldNamingStrategy
)
}
companion object {
private val DEFAULT_NAMING_STRATEGY: FieldNamingStrategy =
PropertyNameFieldNamingStrategy.INSTANCE
}
}
class MyCachingMongoPersistentProperty(
property: Property,
owner: MongoPersistentEntity<*>,
simpleTypeHolder: SimpleTypeHolder,
fieldNamingStrategy: FieldNamingStrategy?
) : CachingMongoPersistentProperty(property, owner, simpleTypeHolder, fieldNamingStrategy) {
override fun isVersionProperty(): Boolean {
return SchemaConstants.VERSION_PROPERTY_NAME == super.getFieldName()
}
}