微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Kotlin 中将 JPA 注释放在委托属性上?

如何解决如何在 Kotlin 中将 JPA 注释放在委托属性上?

假设我有一个 JPA 实体,例如

@Entity
@Table(name = "plasmid_reference")
class PlasmidReference {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int? = null

    @Column(name = "project_id")
    var projectId: Int? = null

    @Column(name = "plasmid_id")
    var plasmidId: Int? = null
}

现在,我知道对于 plasmid_reference 连接表中的每个条目none,这些属性都可以为空。 (实际上,数据库强制执行此操作。)

我如何告诉 Kotlin?

一种天真的方法是尝试使用 lateinit...

@Entity
@Table(name = "plasmid_reference")
class PlasmidReference {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    lateinit var id: Int

    @Column(name = "project_id")
    lateinit var projectId: Int

    @Column(name = "plasmid_id")
    lateinit var plasmidId: Int
}

这甚至无法编译。

基本类型的属性不允许使用'lateinit'修饰符

好的,让我们采用建议的解决方案并转换为使用委托。

@Entity
@Table(name = "plasmid_reference")
class PlasmidReference {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id by notNull<Int>()

    @Column(name = "project_id")
    var projectId by notNull<Int>()

    @Column(name = "plasmid_id")
    var plasmidId by notNull<Int>()
}

这仍然无法编译,因为

此注解不适用于目标'member property with delegate'

好吧……这听起来像是 kotlin 的事情,在 Java 中,我会在字段上扔注释,所以也许……

@Entity
@Table(name = "plasmid_reference")
class PlasmidReference {

    @field:Id
    @field:GeneratedValue(strategy = GenerationType.IDENTITY)
    var id by notNull<Int>()

    @field:Column(name = "project_id")
    var projectId by notNull<Int>()

    @field:Column(name = "plasmid_id")
    var plasmidId by notNull<Int>()
}

不,事实证明,并非如此。仍然无法编译,因为

'@field:' 注释只能应用于具有支持字段的属性

好的,让我们注释“存储委托属性的委托实例的字段”(参见 https://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets

@Entity
@Table(name = "plasmid_reference")
class PlasmidReference {

    @delegate:Id
    @delegate:GeneratedValue(strategy = GenerationType.IDENTITY)
    var id by notNull<Int>()

    @delegate:Column(name = "project_id")
    var projectId by notNull<Int>()

    @delegate:Column(name = "plasmid_id")
    var plasmidId by notNull<Int>()
}

这确实可以编译但会抛出运行时异常,因为

The attribute [plasmidId] is not present in the managed type [EntityTypeImpl@960932033:PlasmidReference 
[ javaType: class com.[...].PlasmidReference descriptor: RelationalDescriptor(com.[...].PlasmidReference --> [DatabaseTable(plasmid_reference)]),mappings: 3]].
    at org.eclipse.persistence.internal.jpa.metamodel.ManagedTypeImpl.getAttribute(ManagedTypeImpl.java:151)
    at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:635)

Aaaaa我没有想法了。

我该如何解决这个问题?

解决方法

将其移动到构造函数定义应该可以解决问题

@Entity
@Table(name = "plasmid_reference")
class PlasmidReference(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int? = null,@Column(name = "project_id")
    var projectId: Int,@Column(name = "plasmid_id")
    var plasmidId: Int
)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。