代码之家  ›  专栏  ›  技术社区  ›  xetra11

Kotlin编写的共享实体库在包含在另一个项目中时会丢失有关默认值的信息

  •  0
  • xetra11  · 技术社区  · 2 年前

    我有一个Kotlin Spring项目,它包含简单的 Entity 模型。我将这个库发布到我的本地maven(和Github包),以便在我的其他项目中重用它们。

    然而,当我将库包含在另一个项目中时,有些东西无法正常工作。

    这是原件 Attachment.kt 实体类

    @Entity
    class Attachment(
        @Id @GeneratedValue var id: Long? = null,
        var url: String? = "",
        @OneToOne
        var author: UserInfo?,
        var fileSize: Long? = 0,
        var fileName: String? = "",
        @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
        var createdAt: DateTime? = DateTime.now()
    )
    

    正如您所看到的,有默认值。 现在,当我包括包含 附件.kt 构造函数给了我一个错误,因为它需要值。这不应该发生,因为存在默认值。

    当我检查包含的实体库的“来源”时,我可以看到以下内容:

    @javax.persistence.Entity public open class Attachment public constructor(id: kotlin.Long? /* = compiled code */, url: kotlin.String? /* = compiled code */, author: com.imhotep.shared.entity.UserInfo?, fileSize: kotlin.Long? /* = compiled code */, fileName: kotlin.String? /* = compiled code */, createdAt: org.joda.time.DateTime? /* = compiled code */) {
        @field:javax.persistence.OneToOne public open var author: com.imhotep.shared.entity.UserInfo? /* compiled code */
    
        @field:org.hibernate.annotations.Type public open var createdAt: org.joda.time.DateTime? /* compiled code */
    
        public open var fileName: kotlin.String? /* compiled code */
    
        public open var fileSize: kotlin.Long? /* compiled code */
    
        @field:javax.persistence.Id @field:javax.persistence.GeneratedValue public open var id: kotlin.Long? /* compiled code */
    
        public open var url: kotlin.String? /* compiled code */
    }
    
    

    由于Java没有默认值,我理解库在这里出现问题的原因。但是我怎样才能做到这一点呢?

    0 回复  |  直到 2 年前
        1
  •  0
  •   Anshul    2 年前

    添加 @JvmOverloads 的注释 Attachment