@CreationTimestamp和@UpdateTimestamp在Kotlin中不起作用 [英] @CreationTimestamp and @UpdateTimestamp don't work in Kotlin

查看:1025
本文介绍了@CreationTimestamp和@UpdateTimestamp在Kotlin中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Tag和Post Entity类:

This is my Tag and Post Entity classes:

@Entity
class Tag(
        @get:NotBlank
        @Column(unique = true)
        val name: String = "",
        val description: String = ""
) {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    val id: Int? = null


    @ManyToMany(mappedBy = "tags")
    val posts: MutableSet<Post> = mutableSetOf()

    @CreationTimestamp
    lateinit var createDate: Date

    @UpdateTimestamp
    lateinit var updateDate: Date

    fun addPost(post: Post) {
        this.posts.add(post)
        post.tags.add(this)
    }


}



@Entity
class Post(
        @get:NotBlank
        var name: String = "",
        val content: String = ""
) {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    val id: Int? = null
    @ManyToMany
    @Cascade(CascadeType.ALL)
    val tags: MutableSet<Tag> = mutableSetOf()

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    lateinit var createDate: Date

    @UpdateTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    lateinit var updateDate: Date


    fun addTag(tag: Tag) {
        this.tags.add(tag)
        tag.posts.add(this)
    }
}

查询:

val post1 = Post( "Post 1", "Post 1 content");
val post2 = Post( "Post 2", "Post 2 content");
var tag = Tag( "news", "Tag description")
post1.addTag(tag)
post2.addTag(tag)
em.persist(post1)
em.persist(post2)
em.remove(post1)
em.flush()

但是,然后,createDate和updateDate返回null(标记和发布):

But then, the createDate and updateDate return null (both tag and post):

我将此代码转换为Java,并且效果很好

I converted this code to Java and it works fine

科特林版本:1.2-M2

Kotlin version: 1.2-M2

springBootVersion:'2.0.0.M7'

springBootVersion: '2.0.0.M7'

推荐答案

问题可能存在于这些注释不限于它们应注释的事实上.这意味着kotlin不知道将它们放在最终字节码中的确切位置.

The problem likely exists in the fact that those annotations are not limited to what they should annotate. This means kotlin does not know exactly where to put them in the final bytecode.

要获得与Java生成的字节码相同的字节码,您需要指定相关注释的注释目标:

To get the same bytecode as would be generated by java, you need to specify the annotation target of the annotation in question:

@field:CreationTimestamp
lateinit var createDate: Date

这篇关于@CreationTimestamp和@UpdateTimestamp在Kotlin中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆