在我的业务逻辑中,我必须处理很多实体 ID,所有这些 ID 都是类型String,这可能会导致混乱,尤其是当您将其中几个作为方法参数传递时。所以我考虑引入一点类型安全内联类。我知道,内联类在 v1.3 中仍然被标记为实验性的。然而,有没有人尝试过使用内联类 as the @Id数据库映射上下文中的属性,在我的例子中是带有 Spring Data 的 MongoDB。
@Entity
class User {
@Id
var id: UserId
}
with
inline class UserId(val id: String)
我猜底层属性没有拆箱,所以 _id 最终会作为数据库中的对象? Spring 的 CrudRepository 接口又如何呢?它似乎可以编译,但最终会起作用:
interface UserRepository : CrudRepository<User, UserId>
可能正在使用AttributeConverter
将内联类转换为原始可能会完成这项工作。有这方面的经验吗?
作为对 @JuergenZimmermann 答案的更新,从版本 3.2(当前是候选版本)开始,Spring Data Commons 就支持这一点:
https://github.com/spring-projects/spring-data-commons/releases/tag/3.2.0-M1 https://github.com/spring-projects/spring-data-commons/releases/tag/3.2.0-M1
然后OP编写的代码应该按照给定的方式工作。更详细的讨论请参见这里:https://github.com/spring-projects/spring-data-commons/pull/2866 https://github.com/spring-projects/spring-data-commons/pull/2866
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)