Kotlin中用于@Id注解属性的内联类

huangapple go评论72阅读模式
英文:

Kotlin inline class for @Id-annotated properties

问题

在我的业务逻辑中,我必须处理大量的实体ID,它们都是String类型的,特别是当您将其中一些作为方法参数传递时,可能会引起混淆。因此,我考虑引入一些内联类以提高类型安全性。我知道,在v1.3中,内联类仍然被标记为实验性的。不过,是否有人尝试过在DB映射上下文中将内联类作为@Id属性,比如在我的情况下是使用Spring Data的MongoDB。

@Entity
class User {
   @Id
   var id: UserId
}

inline class UserId(val id: String)

我猜测底层属性不会自动拆箱,所以_id最终会在数据库中成为一个对象?那么Spring的CrudRepository接口呢?它似乎可以编译,但最终会工作吗:

interface UserRepository : CrudRepository<User, UserId>

也许使用AttributeConverter将内联类转换为原始类型可能会起作用。对此有什么经验吗?

英文:

In my business logic I have to deal with a lot of entity IDs, all of them of type String, which can cause confusion especially when you pass a couple of them as method parameters. So I thought about introducing a little type safety with inline classes. I know, inline classes are still marked as experimental in v1.3. Nevertheless, has anyone ever tried to use an inline class as the @Id property within a DB mapping context, in my case a MongoDB with Spring Data.

@Entity
class User {
   @Id
   var id: UserId
}

with

inline class UserId(val id: String)

I guess there is no unboxing of the underlying property, so _id will end up as an object in the DB? And what about Spring's CrudRepository interfaces? It seems compilable but will it work eventually:

interface UserRepository : CrudRepository&lt;User, UserId&gt;

Probably using AttributeConverter to convert the inline class to a primitive might do the job. Any experiences with this?

答案1

得分: 1

内联类导致完全新的类型,而不仅仅是一个类型别名。即使我们的代码库知道这个新类型是什么,MongoDB 也不知道对吧?因此,您不能直接将内联类存储到相应的原始类型字段中。

英文:

Inline classes result in completely new types, not just a typed Alias. Even if our code base knows what this new type is the MongoDB doesn't right? So you cannot store the inline class directly into the corresponding primitive type Fields

答案2

得分: 0

有一个未解决的 Spring Data Commons 问题:https://github.com/spring-projects/spring-data-commons/issues/1947

英文:

There is an unresolved ticket for Spring Data Commons: https://github.com/spring-projects/spring-data-commons/issues/1947

答案3

得分: 0

作为对@JuergenZimmermann答案的更新,这在Spring Data Commons版本3.2及更高版本中受支持(目前是预发布版本):

https://github.com/spring-projects/spring-data-commons/releases/tag/3.2.0-M1

然后按照原始帖子中的代码编写应该可以正常工作。有关更详细的讨论,请参见此处:https://github.com/spring-projects/spring-data-commons/pull/2866

英文:

As an update to @JuergenZimmermann's answer, this is supported in Spring Data Commons starting from version 3.2 (which is currently pre-release):

https://github.com/spring-projects/spring-data-commons/releases/tag/3.2.0-M1

Then the code as written by the OP should work out as given. For more detailed discussion, see here: https://github.com/spring-projects/spring-data-commons/pull/2866

huangapple
  • 本文由 发表于 2020年1月3日 23:50:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定